PHP (PHP: Hypertext Preprocessor)
What is PHP?
PHP stands for PHP: Hypertext Preprocessor. It is a
web development language written by and for web developers. The product was
originally named Personal Home Page Tools. PHP is an open source, server side,
HTML-embedded web scripting language usually used to create web applications in
combinations with a web server, such as Apache. It enables the user to embed
code fragments in normal HTML-page. It also makes the connection of a web page
to server-side databases. It can also be used to create command-line scripts
akin to Perl or shell scripts.
HTML-embeddedness
PHP can be easily embedded within HTML-page. In
other words, PHP pages are ordinary HTML pages that escape into PHP mode only
when necessary. PHP codes are embedded with the help of canonical php tags.
<? php
?>
Following is
an example-
<html>
<title> Greeting </title>
</head>
<body>
<p> hello,
<? php
//php mode
$firstname=”Raj”;
$secondname=”Kumar”;
echo “$firstname”. ”$secondname”;
?>
This is my first PHP program.
</p>
</body>
</html>
OUTPUT
Hello, Raj Kumar This is my first PHP page.
INCLUDING FILES
A PHP file can be embedded in an HTML page by
putting it in a separate file and calling it by using PHP’s include functions:
·
include (‘/filepath/filename’)
·
require (‘/filepath/filename’)
·
include_once (‘/filepath/filename’)
·
require_once(‘/filepath/filename’)
VARIABLES
To
store information in a php program is by using a variable- a way to name and
hang on to any value that a user wants to use later.
Important
points to know about variables in PHP are-
·
All variables are denoted with a leading
dollar sign.
·
The value of a variable is the value of its
most recent assignment.
·
These are assigned with the = operator, with
expressions on right hand side and variables on left.
·
They can, but do not need to be declared
before assignment.
·
Variables
which are used before they are assigned have default values.
·
They do not have intrinsic type other than
the type of their current value.
Syntax
for assigning a variable is as follow:
·
$my_var = 5;
//integer type
·
$my_var = “Hello world”; //string type
·
$var = 3.56; //float value
CONSTANTS
PHP offers constants which have a single value throughout
their lifetime. They do not have a $ before their names, and by conventions the
names of constants usually are in uppercase letters. They can contain only
scalar values, with global scope.
It is possible to create constants using the
define() function.
define (my_var, 45);
TYPE SUMMARY
PHP has a total eight types: integer, double,
Boolean, strings, arrays, objects, NULL, and resources.
·
Integers are whole numbers, without a
decimal point, like 579.
·
Doubles are floating-point numbers, like
3.1567 or 5.00.
·
Booleans have only two possible values: TRUE
and FALSE.
·
NULL is a special type that has only one
value: NULL.
·
Strings are sequences of characters, like
“hello world”, ‘my name is xyz’.
·
Arrays are named and indexed collections of
other values.
·
Objects are instances of programmer-defined
classes.
·
Resources are special variables that hold
references external to PHP (such as database connection).
OUTPUT
The two most basic constructs for printing to
output are echo and print. Their language status is somewhat confusing because
of their basic constructs of the PHP language rather than being functions. They
can be used either with parentheses or without them.
ECHO
The simplest use of echo is to print a string as
argument. For e.g.
echo “this is my php page”;
Or
echo (“this is my php page”);
We can also give multiple arguments to the
Unparenthesized version of echo but not in Parenthesized version of echo.
Echo “this is my php page”. “Hello world”;
PRINT
The print command is very much similar to echo,
with mainly two important differences-
·
Unlike echo, print can accept only one
argument.
·
Print returns a value, which represents
whether or not the print statement succeeded.
·
The value returned by print is always 1.
print (“3.1567”); //print a string
print (3.1577);
// print a number
USING FUNCTIONS
A
function is a way of wrapping up a chunk of code and giving that chunk a name,
so that the programmer can use that chunk later in just one line of code.
The
most basic syntax of calling (or using) a function is-
function_name
(expression 1, expression 2, …, expression n);
The
function name is followed by a parenthesized and a comma-separated list of
input expressions, they are called arguments to a function. Functions can be
called with zero or more arguments depending on their definition.
Built-in Functions-
The
following are valid calls to built-in PHP functions-
·
sqrt (9); //square root function, returns 3
·
rand (10, 10+10); //random number between 10 and 20
·
strlen (“hello world”); //returns the length of the string, 10
·
pi (); //returns the approximate value of pi
User-defined Functions-
function
function_name ($argument 1, $argument 2, …)
{
Statement
1;
Statement
2;
…
}
Function
definition has four parts-
·
The special word function
·
Name of the function
·
Parameter list- dollar sign separated by commas
·
The function body
Comments