0% found this document useful (0 votes)
6 views

2 Common PHP Script Elements (1)

This document provides an overview of common PHP script elements, including basic syntax, variable declaration, and operators. It covers topics such as variable scope, types of operators (arithmetic, assignment, comparison, etc.), and includes sample code for practical understanding. By the end of the lesson, students should be able to apply PHP syntax and demonstrate skills in using operators and variables.

Uploaded by

scuyno439
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

2 Common PHP Script Elements (1)

This document provides an overview of common PHP script elements, including basic syntax, variable declaration, and operators. It covers topics such as variable scope, types of operators (arithmetic, assignment, comparison, etc.), and includes sample code for practical understanding. By the end of the lesson, students should be able to apply PHP syntax and demonstrate skills in using operators and variables.

Uploaded by

scuyno439
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Common PHP

Script
Elements
At the end of this lesson…

Students will be able to…


• Apply the basic PHP syntax.
• Master the different uses of operators that are
available in PHP.
• Exhibit skills in declaring variable names and
assign values in a variable.
Common PHP Script Elements

Writing PHP Scripts

Basic PHP Syntax


A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>

<?php
// PHP code goes here
?>
Common PHP Script Elements

The default file extension for PHP files is ".php".

Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Common PHP Script Elements

Comments in PHP

- is a line that is not read/executed as part of the program. Its only


purpose is to be read by someone who is looking at the code.

/*
// 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.

Creating (Declaring) PHP Variables


starts with the $ sign, followed by the name of the variable:

<?php
$school = “CSU-Lal-lo”;
$numprograms = 6;
$ave = 90.5;
?>
Common PHP Script Elements

Rules for PHP variables:

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.

Common PHP Script Elements

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.

Common PHP Script Elements

output the sum of two variables:

<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
The PHP echo statement is often used to output data to the screen.

Common PHP Script Elements

Variable Scope

PHP has three different variable scopes:


•local
•global
•static
The PHP echo statement is often used to output data to the screen.

Common PHP Script Elements

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();

echo "<p>Variable x outside function is: $x</p>";


?>
The PHP echo statement is often used to output data to the screen.

Common PHP Script Elements

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();

// using x outside the function will generate an error


echo "<p>Variable x outside function is: $x</p>";
?>
The PHP echo statement is often used to output data to the screen.

PHP Operators

PHP Operators
Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:


Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
Conditional assignment operators
The PHP echo statement is often used to output data to the screen.

PHP Mathematical Operators

The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction,
multiplication etc.
Operator Name Example Result

+ Addition $x + $y Sum of $x and $y

- Subtraction $x - $y Difference of $x and $y

* Multiplication $x * $y Product of $x and $y

/ Division $x / $y Quotient of $x and $y

% Modulus $x % $y Remainder of $x divided by $y

** Exponentiation $x ** $y Result of raising $x to the $y'th


power
The PHP echo statement is often used to output data to the screen.

Sample Program

<!DOCTYPE html> echo "$x + $y = $sum<br>";


<html> echo "$x - $y = $diff<br>";
<body> echo "$x * $y = $mul<br>";
echo "$x / $y = $quo<br>";
<?php echo "The remainder of $x / $y is $rem<br>";
$x = 25; echo "$x raised to the $y = $exp";
$y = 5; ?>
$sum=$x + $y; </body>
$diff=$x - $y; </html>
$mul=$x * $y;
$quo=$x / $y;
$rem=$x % $y;
$exp=$x**$y;
The PHP echo statement is often used to output data to the screen.

PHP Assignment Operators


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.
Assignment Same as... Description

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

<!DOCTYPE html> $div=25;


<html> $div/=5;
<body>
$rem=45;
$rem%=4;
<?php
$add=20; echo"SUM:$add<br>";
$add+=100; echo"DIFFERENCE:$sub<br>";
echo"PRODUCT:$mul<br>";
$sub=50; echo"QUOTIENT:$div<br>";
$sub-=25; echo"REMAINDER:$rem<br>";
?>
$mul=5;
</body>
$mul*=10;
</html>
The PHP echo statement is often used to output data to the screen.

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

The PHP increment operators are used to increment a variable's value.


The PHP decrement operators are used to decrement a variable's value.

Operator Name Description


++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post- Returns $x, then decrements $x by one
decrement
The PHP echo statement is often used to output data to the screen.

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

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result

and And $x and $y True if both $x and $y are true

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
&& And $x && $y True if both $x and $y are true

|| Or $x || $y True if either $x or $y is true

! Not !$x True if $x is not true


The PHP echo statement is often used to output data to the screen.

String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result


. Concatenation $txt1 . $txt2 Concatenation
of $txt1 and
$txt2
.= Concatenation $txt1 .= $txt2 Appends $txt2
assignment to $txt1
The PHP echo statement is often used to output data to the screen.

Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result


+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same
key/value pairs
=== Identity $x === $y Returns true if $x and $y have the same
key/value pairs in the same order and of
the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y
The PHP echo statement is often used to output data to the screen.

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result

?: Ternary $x Returns the value of $x.


= expr1 ? expr2 : expr3 The value of $x is expr2 if expr1 = TRUE.
The value of $x is expr3 if expr1 = FALSE

?? Null $x = expr1 ?? expr2 Returns the value of $x.


coalescing 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
THANK YOU!

You might also like