2 Common PHP Script Elements (1)
2 Common PHP Script Elements (1)
Script
Elements
At the end of this lesson…
<?php
// PHP code goes here
?>
Common PHP Script Elements
Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Common PHP Script Elements
Comments in PHP
/*
// This is a single-line comment This is a multiple-lines comment block
that spans over multiple
# This is also a single-line lines
comment */
Common PHP Script Elements
Variables
- "containers" for storing information.
<?php
$school = “CSU-Lal-lo”;
$numprograms = 6;
$ave = 90.5;
?>
Common PHP Script Elements
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)
Remember that PHP variable names are case-sensitive!
The PHP echo statement is often used to output data to the screen.
Output Variables
The PHP echo statement is often used to output data to the
screen.
<?php
$school = “CSU-Lal-lo";
echo "I love $school!";
?>
The PHP echo statement is often used to output data to the screen.
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
The PHP echo statement is often used to output data to the screen.
Variable Scope
Global Scope
A variable declared outside a function has a GLOBAL SCOPE and can only be accessed
outside a function:
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate an error
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
Local Scope
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
PHP Operators
PHP Operators
Operators are used to perform operations on variables and values.
The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction,
multiplication etc.
Operator Name Example Result
Sample Program
x=y x=y The left operand gets set to the value of the 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 echo statement is often used to output data to the screen.
Sample Program
Comparison Operators
Operato Name Example Result
r
== Equal $x == $y
Returns true if $x is equal to $y
=== Identical $x ===
Returns true if $x is equal to $y, and they are of the
$y same type
!= Not equal $x
!= $y Returns true if $x is not equal to $y
<> Not equal $x
<> $y Returns true if $x is not equal to $y
!== Not identical $x
!== $y Returns true if $x is not equal to $y, or they are not of
the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal $x >= $y Returns true if $x is greater than or equal to $y
to
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<=> Spaceship $x <=> Returns an integer less than, equal to, or greater than
The PHP echo statement is often used to output data to the screen.
Sample Program
The PHP echo statement is often used to output data to the screen.
Increment/Decrement Operators
Sample Program
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
echo $x++;
echo --$x;
echo $x--;
?>
</body>
</html>
The PHP echo statement is often used to output data to the screen.
Logical Operators
String Operators
PHP has two operators that are specially designed for strings.
Array Operators
The PHP conditional assignment operators are used to set a value depending on conditions: