<?php
// place PHP code here
?>
//
β Denotes one line comment
#
β Another way of one line comment
/*...*/
β Everything between /* and */ is not executed, also works across several lines
echo "<h1>hamada</h1>";
echo and print are more or less the same. They are both used to output data to the screen.
echo vs print
echo has no return value print has a return value of 1 so it can be used in expressions.
echo can take multiple parameters print can take one argument.
echo is marginally faster than print.
<?php
$name = "hamada";
?>
Variables need to start with a letter or underscore (_)
PHP variables are case sensitive
If your variable consists of more than one word either write it $my_variable or $myVariable
- Integers β Integers are non-decimal numbers between -2,147,483,648 and ,147,483,647 .
- Floats β This is the name for numbers with a decimal point or in exponential form.
- Strings β means text .
- Boolean - True or False
- Arrays β Arrays are variables that store several values.
- Objects β Objects store both data and information on how to process it.
- Resources β These are references to functions and resources outside of PHP.
- NULL β A variable that is NULL doesnβt have any value.
$GLOBALS
β Used to access global variables from anywhere inside a PHP script$_SERVER
β Contains information about the locations of headers, paths, and scripts$_GET
β Can collect data that was sent in the URL or submitted in an HTML form$_POST
β Used to gather data from an HTML form and to pass variables$_REQUEST
β Also collects data after submitting an HTML form
define(name, value, true/false)
- LINE β Denotes the number of the current line in a file
- FILE β Is the full path and filename of the file
- DIR β The directory of the file
In PHP there are different types of arrays:
- Indexed arrays β Arrays that have a numeric index
- Associative arrays β Arrays where the keys are named
- Multidimensional arrays β Arrays that contain one or more other arrays
<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
<?php
$human = array("hamada"=>"35", "ahmed"=>"37", "abdo"=>"43");
foreach($human as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
<?php
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
for ($row = 0; $row < 4; $row++) {
echo "<p><b>Row number $row</b></p>";
echo "<ul>";
for ($col = 0; $col < 3; $col++) {
echo "<li>".$cars[$row][$col]."</li>";
}
echo "</ul>";
}
?>
- A function is a block of statements that can be used repeatedly in a program.
- A function will not execute automatically when a page loads.
- A function will be executed by a call to the function.
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the function
?>
+ β Addition
- β Subtraction
* β Multiplication
/ β Division
% β Modulo (the remainder of value divided by another)
** β Exponentiation
+= ----> a += b is the same as a = a + b
-= ----> a -= b is the same as a = a β b
*= ----> a *= b is the same as a = a * b
/= ----> a /= b is the same as a = a / b
%= ----> a %= b is the same as a = a % b
== β Equal
=== β Identical
!= β Not equal
<> β Not equal
!== β Not identical
< β Less than
> β Greater than
<= β Less than or equal to
>= β Greater than or equal to
<=> β Less than, equal to, or greater than
and β And
or β Or
xor β Exclusive or
! β Not
&& β And
|| β Or
++$v β Increments a variable by one, then returns it
$v++ β Returns a variable, then increments it by one
--$v β Decrements the variable by one, returns it afterward
$v-- β Returns the variable then decrements it by one
<?php
$x = 0;
while($x <= 100) {
echo "The number is: $x <br>";
$x+=10;
}
?>
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
<?php
for ($x = 0; $x <= 100; $x+=10) {
echo "The number is: $x <br>";
}
?>
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
- If Statement
if (condition) {
// code to execute if condition is met
}
- Ifβ¦Else
if (condition) {
// code to execute if condition is met
} else {
// code to execute if condition is not met
}
- Switch Statement
switch (n) {
case x:
code to execute if n=x;
break;
case y:
code to execute if n=y;
break;
case z:
code to execute if n=z;
break;
// add more cases as needed
default:
code to execute if n is neither of the above;
}