Professional Documents
Culture Documents
1: <?php
2: echo “<h1>Hello Web!</h1>”;
3: ?>
Each code line in PHP must end with a semicolon.
be parsed by the
PHP engine
*/
VARIABLES
A variable is a special container that you can define, which
then “holds” a value, such as a number, string, or array.
When a variable is set it can be used over and over again in
your script
Without variables, you would be forced to hard-code each
specific value used in your scripts.
All variable consists of a name of your choosing, preceded by
a dollar sign ($).
The correct way of setting a variable in PHP:
$var_name = value;
CONT.…
In PHP a variable does not need to be declared
before being set.
In the PHP you do not have to tell which data type
the variable is.
PHP automatically converts the variable to the
correct data type, depending on how they are set.
In a strongly typed programming language, you
have to declare (define) the type and name of the
variable before using it.
VARIABLE NAMING RULES
Variable names can include letters, numbers, and the
underscore character (_).
A variable name must start with a letter or an underscore "_"
Example: - <?php
$txt="Hello World";
echo $txt;
$num1 = 8;
echo 8;
$letter = s;
echo letter; ?>
OPERATORS AND EXPRESSIONS
Operator is a symbol or series of symbols that, when used in
conjunction with values, performs an action, and usually
produces a new value.
A value operated on by an operator is referred to as an
operand.
An operand is a value used in conjunction with an
operator.
Example: (4 + 5)
The integers 4 and 5 are operands.
The addition operator (+) operates on these operands to
produce the integer 9.
The combination of operands with an operator to produce a
result is called an expression.
ARITHMETIC OPERATORS
Operator Description
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
++ Increment
-- Decrement
COMPARISON OPERATORS
Operator Description
== equal to .
=== equal value and equal type
!= not equal
!== not equal value or not equal type
+= x += y x=x+y
-= x -= y x=x-y
.
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
LOGICAL OPERATORS
Operator Description
&&
logical and .
|| logical or
! logical not
.
OPERATOR PRECEDENCE
STRING OPERATOR
The concatenation operator (.)
It is used to put two string values together.
use the dot (.) operator:
Example:
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
Output:
<?php
echo strlen("Hello world!");
?>
The output of the code above will be:
12
CONT.…
The strpos() function:
It is used to search for a string or character.
If a match is found in the string, this function will return
the position of the first match.
If no match is found, it will return FALSE.
Example:
<?php
echo strpos("Hello world!","world");
?>
The output will be: 6
The position is 6, and not 7, because the first position in the
string is 0, and not 1.
CONT.:
The str_replace():
It replaces all occurrences of the search text within the
target string.
Example
<?php
$my_str = 'If the facts do not fit the theory, change the
facts.';
echo str_replace("facts", "truth", $my_str);
?>
The output of the above code will be:
If the truth do not fit the theory, change the
truth.
COMPLETE PHP STRING REFERENCE:
http://w3schools.com/php/php_ref_string.asp
The if statement
The if...else statement
The if...elseif....else statement
The switch...case statement
The if statement
The if statement is used to execute a block of code only if the
specified condition evaluates to true.
This is the simplest PHP's conditional statements and can be written
like:
if(condition){
// Code to be executed
}
The following example will output "Have a nice weekend!" if the
current day is Friday:
<?php
$d = “Fri";
if($d == "Fri"){
echo "Have a nice weekend!";
}
?>
The if...else Statement
The if...else statement allows you to execute one block of code if
the specified condition is evaluates to true and another block of
code if it is evaluates to false.
It can be written, like this:
if(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}
Example
<?php $d = “Fri";
if($d == “Fri")
{ echo "Have a nice weekend!"; }
else { echo "Have a nice day!"; }
?>
The if...elseif...else Statement
The if...elseif...else a special statement that is used to combine multiple
if...else statements.
if(condition){
// Code to be executed if condition is true
} elseif(condition){
// Code to be executed if condition is true
} else{
// Code to be executed if condition is false
}
Example
<?php
$d = “Fri";
if($d == "Fri")
{ echo "Have a nice weekend!"; }
elseif($d == "Sun") { echo "Have a nice Sunday!"; }
else { echo "Have a nice day!"; }
?>
PHP Switch…Case Statements
Theswitch-case statement tests a variable against a series of
values until it finds a match, and then executes the block of
code corresponding to that match.
switch(n){
case label1:
// Code to be executed if n=label1
break;
case label2:
// Code to be executed if n=label2
break;
...
default:
// Code to be executed if n is different from all labels
}
PHP Switch…Case Statements. . . Example
<?php case "Fri":
$today = “Mon"; echo "Today is Friday. The
switch($today){ weekend! .";
case "Mon": break;
echo "Today is Monday. Clean your case "Sat":
dorm."; echo "Today is Saturday. Its movie
break; time.";
case "Tue": break;
echo "Today is Tuesday. Buy some case "Sun":
food."; echo "Today is Sunday. Do some
break; rest.";
case "Wed": break;
echo "Today is Wednesday. AIP Class default:
."; echo "No information available.";
break; break;
case "Thu": }
echo "Today is Thursday. Clean ?>
your Lab.";
break;
CONT.…
Php switch statements
Case: it takes a single variable as input and then checks it against all the
different cases you set up for that switch statement (e.g. case November:
Output: Current Month!)
Break (to stop executing all the cases that follow the correct case)
Default case (when the variable doesn’t match all conditions, no case
before default default:)
Loops in PHP
Scripts can decide how many times to execute a block of
code.
Loop statements are designed to enable you to achieve
repetitive tasks.
A loop will continue to operate until a condition is
achieved, or you explicitly choose to exit the loop.
Loops are used frequently in scripts to set up a block of
statements that repeat.
CONT.…
PHP supports following four loop types.
for - loops through a block of code a specified number
of times.
while - loops through a block of code if and as long as
a specified condition is true.
do...while - loops through a block of code once, and
then repeats the loop as long as a special condition is
true.
foreach - loops through a block of code for each
element in an array.
For Loops in PHP
It is used when you know how many times you want to execute
a statement or a block of statements.
Syntax: for (initialization; condition; increment)
{
code to be executed;
}
While Loops in PHP
The while statement will execute a block of code if and as long
as a test expression is true.
If the test expression is true then the code block will be executed
and the loop will continue until the test expression is found to be
false.
Syntax: while (condition is true) {
code to be executed;
}
Do…while Loops in PHP
It will always execute the block of code once, it will
then check the condition, and repeat the loop while the
specified condition is true.
Syntax do {
code to be executed;
} while (condition is true);
Foreach Loops in PHP
The foreach statement is used to loop through arrays.
For each pass the value of the current array element is assigned
to $value and the array pointer is moved by one and in the next
pass next element will be processed.
Syntax
<?php
function bighello()
{ echo “<h1>HELLO!</h1>”; }
bighello();
?>
CONT.…
Declaring a Function That Requires an Argument
<?php
function printBR($txt)
{
echo $txt.”<br/>”;
}
printBR(“This is a line.”);
?>
CONT.…
The following function has two parameters:
<?php
function writeMyName($fname,$punctuation)
{
}
writeMyName("John",".");
writeMyName("Sarah","!");
writeMyName("Smith","...");
?>
PHP FUNCTIONS - RETURN VALUES
Functions can also be used to return values.
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
?>
</body>
</html>
PHP ARRAYS
An array can store one or more values in a single
variable name.
An array can hold multiple, separate pieces of
information.
It is therefore like a list of values, each value being a
string or a number or even another array.
For each item in the list, there is a key (or index)
associated with it
An array follows the same naming rules as any other
variable.
CONT.…
Arrays are indexed, which means that each entry is
made up of a key and a value.
The key is the index position, beginning with 0 and
increasing incrementally by 1 with each new element in
the array.
The value is whatever value you associate with that
position—a string, an integer, or whatever you want.
CREATING ARRAYS
You can create an array using either the array( ) function or the
array operator [ ].
The array( ) function is usually used when you want to create a
new array and populate it with more than one element, all in one
fell swoop.
$rainbow = array(“red”, “orange”, “yellow”, “green”, “blue”,
“indigo”)
The array operator is often used when you want to create a new
array with just one element at the outset, or when you want to add
to an existing array element.
$rainbow[ ] = “red”;
$rainbow[ ] = “orange”;
$rainbow[ ] = “yellow”; cont…
TYPES OF ARRAYS
There are three different kind of arrays.
In php there are three types of arrays:
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbours";
?>
The
code above will output:
Quagmire and Joe are Peter's neighbours
ASSOCIATIVE ARRAYS
When storing data about specific named values, a numerical
array is not always the best way to do it.
With associative arrays we can use the values as keys and
assign values to them.
Example 1: we use an array to assign ages to the different
persons:
$ages = array("Peter"=>32, "Quagmire"=>30,
"Joe"=>34);
Example 2: shows a different way of creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
CONT.…
Example:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>