PHP Baiscs
PHP Baiscs
● HTML
● CSS
● JavaScript
What is PHP?
● PHP files can contain text, HTML, CSS, JavaScript, and PHP code
● PHP code is executed on the server, and the result is returned to the
browser as plain HTML
● PHP files have extension ".php"
Why PHP?
<?php
?>
Below, we have an example of a simple PHP file, with a PHP script that uses a
built-in PHP function "echo" to output the text "Hello World!" on a web page:
A simple .php file with both HTML code and PHP code:
<!DOCTYPE html>
<html>
<body>
?>
</body>
</html>
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and
user-defined functions are not case-sensitive.
In the example below, all three echo statements below are equal and legal:
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
Note: However; all variable names are case-sensitive!
Look at the example below; only the first statement will display the value of
the $color variable! This is because $color, $COLOR, and $coLOR are
treated as three different variables:
<!DOCTYPE html>
<html>
<body
<?php
$color = "red";
?>
</body>
</html>
PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the
variable:
$id = 5;
$name = "alfiya";
In the example above, the variable $id will hold the value 5, and the variable
$name will hold the value "John".
Note: When you assign a text value to a variable, put quotes around the
value.
A variable can have a short name (like $x and $y) or a more descriptive
name ($age, $carname, $total_volume).
● 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 ($age and $AGE are two different
variables)
Output Variables
The PHP echo statement is often used to output data to the screen.
The following example will show how to output text and a variable:
Example
$txt = "PHP";
echo "I love $txt!";
● local
● global
● static
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function:
Example
$x = 5; // global scope
function myTest() {
myTest();
$x = 5; // global scope
function myTest() {
myTest();
echo and print are more or less the same. They are both used to output
data to the screen.
The differences are small: echo has no return value while print has a return
value of 1 so it can be used in expressions. echo can take multiple
parameters (although such usage is rare) while print can take one
argument. echo is marginally faster than print.
echo "Hello";
//same as:
echo("Hello");
The following example shows how to output text with the echo command
(notice that the text can contain HTML markup):
Variables can store data of different types, and different data types can do
different things.
● String
● Integer
● Float (floating point numbers - also called double)
● Boolean
● Array
● Object
● NULL
● Resource
PHP Constants
A valid constant name starts with a letter or underscore (no $ sign before the
constant name).
Note: Unlike variables, constants are automatically global across the entire
script.
Syntax
define(name, value);
Parameters:
PHP Operators
● Arithmetic operators
● Assignment operators
● Comparison operators
● Increment/Decrement operators
● Logical operators
● String operators
● Array operators
● Conditional assignment operators
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction, multiplication
etc.
% Modulus $x % $y Remainder of $x
divided by $y
The PHP assignment operators are used with numeric values to write a value
to a variable.
The basic assignment operator in PHP is "=". It means that the left operand
gets set to the value of the assignment expression on the right.
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division
x %= y x=x%y Modulus
The PHP comparison operators are used to compare two values (number or
string):
or Or $x or $y True if either $x
or $y is true
xor Xor $x xor $y True if either $x
or $y is true, but
not both
|| Or $x || $y True if either $x
or $y is true
PHP has two operators that are specially designed for strings.
The PHP conditional assignment operators are used to set a value depending
on conditions:
Operator Name Example Result
The value of $x
is expr2 if expr1
= TRUE.
The value of $x
is expr3 if expr1
= FALSE
The value of $x
is expr1 if expr1
exists, and is not
NULL.
If expr1 does
not exist, or is
NULL, the value
of $x is expr2.
Introduced in
PHP 7
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.
Syntax
if (condition) {
if (5 > 3) {
Syntax
if (condition) {
} else {
}
Output "Have a good day!" if the current time is less than 20, and "Have a good
night!" otherwise:
$t = date("H");
} else {
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!":
$t = date("H");
} else {
Nested If
$a = 13;
} else {
PHP Loops
Often when you write code, you want the same block of code to run over and
over again a certain number of times. So, instead of adding several almost
equal code-lines in a script, we can use loops.
Loops are used to execute the same block of code again and again, as long
as a certain condition is true.
The while loop executes a block of code as long as the specified condition is
true.
Print $i as long as $i is less than 6:
$i = 1;
echo $i;
$i++;
The do...while loop will always execute the block of code at least once, it
will then check the condition, and repeat the loop while the specified
condition is true.
$i = 1;
do {
echo $i;
$i++;
The for loop is used when you know how many times the script should run.
1. The first expression, $x = 0;, is evaluated once and sets a counter to
0.
2. The second expression, $x <= 10;, is evaluated before each iteration,
and the code block is only executed if this expression evaluates to
true. In this example the expression is true as long as $x is less than,
or equal to, 10.
3. The third expression, $x++;, is evaluated after each iteration, and in
this example, the expression increases the value of $x by one at each
iteration.
if ($x == 4) {
break;
}
Continue in For Loops
The continue statement stops the current iteration in the for loop and
continue with the next.
if ($x == 4) {
continue;