PHP
Learning PHP
By: Eyad Alshareef
Eyad Alshareef
19.1 Introduction
PHP: Hypertext Preprocessor, has become the
most popular server-side scripting language for
creating dynamic web pages.
PHP is
Eyad Alshareef
open source.
platform independentimplementations exist for
all major UNIX, Linux, Mac and Windows
operating systems.
supports a large number of databases.
Basic PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with<?phpand ends with?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
A PHP file normally contains HTML tags, and some PHP
scripting code.
PHP statements terminate with a semicolon (;).
Eyad Alshareef
Example 1
Eyad Alshareef
Comments in PHP
A comment in PHP code is a line that is not read/executed as part of the
program. Its only purpose is to be read by someone who is editing the code!
Eyad Alshareef
PHP Case Sensitivity
In PHP, all user-defined functions, classes, and keywords (e.g. if, else,
while, echo, etc.) are NOT case-sensitive.
In the example below, all three echo statements below are legal (and
equal):
in PHP, all variables are case-sensitive.
Eyad Alshareef
PHP 5Variables
Variables are "containers" for storing information:
Rules for PHP variables:
A variable starts with the $ sign, followed by the name of the variable
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ )
Variable names are case sensitive ($y and $Y are two different
variables)
Remember that PHP variable names are case-sensitive!
Eyad Alshareef
Creating (Declaring) PHP Variables
PHP has no command for declaring a variable.
A variable is created the moment you first assign a value to it:
PHP is a Loosely Typed Language
PHP automatically converts the variable to the correct data type,
depending on its value.
Eyad Alshareef
19.2 A Simple PHP Program
When a variable is encountered inside a double-quoted ("")
string, PHP interpolates the variable. In other words, PHP
inserts the variables value where the variable name appears
in the string.
All
operations requiring PHP interpolation execute
on the server before the HTML5 document is sent to
the client.
Eyad Alshareef
Eyad Alshareef
Eyad Alshareef
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a variable is the part of the script where the variable can be
referenced/used.
PHP has three different variable scopes:
local
global
static
Eyad Alshareef
PHP 5echo and print Statements
There are some differences between echo and print:
echo - can output one or more strings
print - can only output one string, and returns always 1
Eyad Alshareef
PHP Strings
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
Eyad Alshareef
PHP Integers
An integer is a number without decimals.
Rules for integers:
An integer must have at least one digit (0-9)
An integer cannot contain comma or blanks
An integer must not have a decimal point
An integer can be either positive or negative
Integers can be specified in three formats: decimal (10-based), hexadecimal (16based - prefixed with 0x) or octal (8-based - prefixed with 0)
The PHP var_dump() function returns the data type and value of variables
Eyad Alshareef
PHP Floating Point Numbers
A
floating point number is a number with a
decimal point or a number in exponential
form.
PHP
Booleans
Booleans
$x=true;
$y=false;
Eyad Alshareef
can be either TRUE or FALSE.
PHP NULL Value
The special NULL value represents that a variable has no value. NULL is the
only possible value of data type NULL.
The NULL value identifies whether a variable is empty or not. Also useful to
differentiate between the empty string and null values of databases.
Variables can be emptied by setting the value to NULL:
Eyad Alshareef
PHP 5Operators
Eyad Alshareef
Eyad Alshareef
Eyad Alshareef
Eyad Alshareef
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Eyad Alshareef
Switch
Eyad Alshareef
While Loops
While (condition)
{
Statements;
}
<?php
$count=0;
While($count<3)
{
Print hello PHP. ;
$count += 1;
// $count = $count + 1;
// or
// $count++;
?>
hello PHP. hello PHP. hello PHP.
Eyad Alshareef
Functions
Functions MUST be defined before the calling
Function headers are of the format
function functionName($arg_1, $arg_2, , $arg_n)
Note that no return type is specified
Unlike variables, function names are not case sensitive (foo() ==
Foo() == FoO())
Eyad Alshareef
Functions example
<?php
function sum($x,$y) {
$z=$x+$y;
return $z;
}
echo "5 + 10 = " . sum(5,10) . "<br>";
echo "7 + 13 = " . sum(7,13) . "<br>";
echo "2 + 4 = " . sum(2,4);
?>
Eyad Alshareef
19.3 Converting Between Data Types
Type conversions can be performed using function settype. This
function takes two argumentsa variable whose type is to be changed
and the variables new type.
Variables are typed based on the values assigned to them.
Function gettype returns the current type of its argument.
Calling function settype can result in loss of data. For example,
doubles are truncated when they are converted to integers.
When converting from a string to a number, PHP uses the value of the
number that appears at the beginning of the string. If no number
appears at the beginning, the string evaluates to 0.
Eyad Alshareef
19.3 Converting Between Data Types
Another option for conversion between types is casting (or
type casting). Casting does not change a variables
contentit creates a temporary copy of a variables value
in memory.
The concatenation operator (.) combines multiple
strings.
A print statement split over multiple lines prints all the
data that is enclosed in its parentheses.
Eyad Alshareef
Eyad Alshareef
Eyad Alshareef