PHP tutorial : PHP 5 - Syntax, Variables Function in detail - part 2
later we will start about array usage------------------------------------------------
demo_oper_multiplication2
Assignment operator: x *= y
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$x *= 6;
echo $x;
?>
</body>
</html>
*********** 30
___________________________________________________
demo_oper_division2
Assignment operator: x /= y
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$x /= 5;
echo $x;
?>
</body>
</html>
*********** 2
___________________________________________________
demo_oper_modulus2
Assignment operator: x %= y
<!DOCTYPE html>
<html>
<body>
<?php
$x = 15;
$x %= 4;
echo $x;
?>
</body>
</html>
*********** 3
___________________________________________________
demo_oper_equal
Comparison operator: Equal (==)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_identical
Comparison operator: Identical (===)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x === $y); // returns false because types are not equal
?>
</body>
</html>
*********** bool(false)
___________________________________________________
demo_oper_not_equal
Comparison operator: Not equal (!=)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values are equal
?>
</body>
</html>
*********** bool(false)
___________________________________________________
demo_oper_not_equal2
Comparison operator: Not equal (<>)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x <> $y); // returns false because values are equal
?>
</body>
</html>
*********** bool(false)
___________________________________________________
demo_oper_not_identical
Comparison operator: Not identical (!==)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x !== $y); // returns true because types are not equal
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_greater_than
Comparison operator: Greater than (>)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
var_dump($x > $y); // returns true because $x is greater than $y
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_less_than
Comparison operator: Less than (<)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 50;
var_dump($x < $y); // returns true because $x is less than $y
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_greater_than2
Comparison operator: Greater than or equal (>=)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 50;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_less_than2
Comparison operator: Less than or equal (<=)
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 50;
var_dump($x <= $y); // returns true because $x is less than or equal to $y
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_arr_union
Array operator: Union (+)
+ Union $x + $y Union of $x and $y
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
print_r($x + $y); // union of $x and $y
?>
</body>
</html>
*********** Array ( [a] => red [b] => green [c] => blue [d] => yellow )
___________________________________________________
demo_oper_arr_equality
Array operator: Equality (==)
== Equality $x == $y Returns true if $x and $y have the same key/value pairs
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x == $y);
?>
</body>
</html>
*********** bool(false)
___________________________________________________
demo_oper_arr_identity
Array operator: Identity (===)
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x === $y);
?>
</body>
</html>
*********** bool(false)
___________________________________________________
demo_oper_arr_inequality
Array operator: Inequality (!=)
!= Inequality $x != $y Returns true if $x is not equal to $y
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x != $y);
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_arr_inequality2
Array operator: Inequality (<>)
<> Inequality $x <> $y Returns true if $x is not equal to $y
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x <> $y);
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_arr_non_identity
Array operator: Non-identity (!==)
!== Non-identity $x !== $y Returns true if $x is not identical to $y
<!DOCTYPE html>
<html>
<body>
<?php
$x = array("a" => "red", "b" => "green");
$y = array("c" => "blue", "d" => "yellow");
var_dump($x !== $y);
?>
</body>
</html>
*********** bool(true)
___________________________________________________
demo_oper_string1
PHP String Operators
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Hello";
$txt2 = " world!";
echo $txt1 . $txt2;
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_string2
PHP String Operators
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1
<!DOCTYPE html>
<html>
<body>
<?php
$txt1 = "Hello";
$txt2 = " world!";
$txt1 .= $txt2;
echo $txt1;
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_and
Logical operator: and
and And $x and $y True if both $x and $y are true
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_or
Logical operator: or
or Or $x or $y True if either $x or $y is true
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 or $y == 80) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_xor
Logical operator: xor
xor Xor $x xor $y True if either $x or $y is true, but not both
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 xor $y == 80) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_and2
Logical operator: && (and)
&& And $x && $y True if both $x and $y are true
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 && $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_or2
Logical operator: || (or)
|| Or $x || $y True if either $x or $y is true
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = 50;
if ($x == 100 || $y == 80) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_not
Logical operator: not
! Not !$x True if $x is not true
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
if ($x !== 90) {
echo "Hello world!";
}
?>
</body>
</html>
*********** Hello world!
___________________________________________________
demo_oper_pre_incr
Increment operator: ++$x
++$x Pre-increment Increments $x by one, then returns $x
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
?>
</body>
</html>
*********** 11
___________________________________________________
demo_oper_post_incr
Increment operator: $x++
$x++ Post-increment Returns $x, then increments $x by one
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x++;
?>
</body>
</html>
*********** 10
___________________________________________________
demo_oper_pre_decr
Decrement operator: --$x
--$x Pre-decrement Decrements $x by one, then returns $x
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo --$x;
?>
</body>
</html>
*********** 9
___________________________________________________
demo_oper_post_decr
Decrement operator: $x--
$x-- Post-decrement Returns $x, then decrements $x by one
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x--;
?>
</body>
</html>
*********** 10
___________________________________________________
___________________________________________________
PHP 5 if...else...elseif Statements
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
if statement - executes some code if one condition is true
if...else statement - executes some code if a condition is true and another code if that condition is false
if...elseif....else statement - executes different codes for more than two conditions
switch statement - selects one of many blocks of code to be executed
PHP - The if Statement
The if statement executes some code if one condition is true.
Syntax
if (condition) {
code to be executed if condition is true;
}
The example below will output "Have a good day!" if the current time (HOUR) is less than 20:
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
PHP - The if...else Statement
The if....else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
The example below will output "Have a good day!" if the current time is less than 20, and "Have a good night!" otherwise:
Example
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The if...elseif....else Statement
The if....elseif...else statement executes different codes for more than two conditions.
Syntax
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
The example below will output "Have a good morning!" if the current time is less than 10, and "Have a good day!" if the current time is less than 20. Otherwise it will output "Have a good night!":
Example
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
PHP - The switch Statement
The switch statement will be explained in the next chapter.
___________________________________________________
demo_if
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
}
?>
</body>
</html>
*********** Have a good day!
___________________________________________________
demo_if_else
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
*********** Have a good day!
___________________________________________________
demo_if_elseif
<!DOCTYPE html>
<html>
<body>
<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
***********
The hour (of the server) is 04, and will give the following message:
Have a good morning!
___________________________________________________
PHP 5 switch Statement
The switch statement is used to perform different actions based on different conditions.
The PHP switch Statement
Use the switch statement to select one of many blocks of code to be executed.
Syntax
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
This is how it works: First we have a single expression n (most often a variable), that is evaluated once. The value of the expression is then compared with the values for each case in the structure. If there is a match, the block of code associated with that case is executed. Use break to prevent the code from running into the next case automatically. The default statement is used if no match is found.
Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
___________________________________________________
demo_switch
<!DOCTYPE html>
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>
*********** Your favorite color is red!
___________________________________________________
PHP 5 while Loops
PHP while loops execute a block of code while the specified condition is true.
PHP Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this.
In PHP, we have the following looping statements:
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
for - loops through a block of code a specified number of times
foreach - loops through a block of code for each element in an array
The PHP while Loop
The while loop executes a block of code as long as the specified condition is true.
Syntax
while (condition is true) {
code to be executed;
}
The example below first sets a variable $x to 1 ($x = 1). Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5). $x will increase by 1 each time the loop runs ($x++):
Example
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
The PHP do...while Loop
The do...while loop 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);
The example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:
Example
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Notice that in a do while loop the condition is tested AFTER executing the statements within the loop. This means that the do while loop would execute its statements at least once, even if the condition is false the first time.
The example below sets the $x variable to 6, then it runs the loop, and then the condition is checked:
Example
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
The for loop and the foreach loop will be explained in the next chapter.
___________________________________________________
demo_loop_while
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
***********
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
___________________________________________________
demo_loop_do_while
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
***********
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
___________________________________________________
demo_loop_do_while2
<!DOCTYPE html>
<html>
<body>
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
</body>
</html>
*********** The number is: 6
___________________________________________________
PHP 5 for Loops
PHP for loops execute a block of code a specified number of times.
The PHP for Loop
The for loop is used when you know in advance how many times the script should run.
Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}
Parameters:
init counter: Initialize the loop counter value
test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
increment counter: Increases the loop counter value
The example below displays the numbers from 0 to 10:
Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
The PHP foreach Loop
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.
Syntax
foreach ($array as $value) {
code to be executed;
}
For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element.
The following example demonstrates a loop that will output the values of the given array ($colors):
Example
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
You will learn more about arrays in a later chapter.
___________________________________________________
demo_loop_for
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
***********
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
___________________________________________________
demo_loop_foreach
<!DOCTYPE html>
<html>
<body>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>
***********
red
green
blue
yellow
___________________________________________________
reference
http://adf.ly/1hAVfk
http://adf.ly/1hAVkn
http://adf.ly/1hAVns
http://adf.ly/1hAVqr
http://adf.ly/1hAVvX

No comments:
Post a Comment
Note: only a member of this blog may post a comment.