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

Chapter 11 PHP.

The document provides an overview of the basic fundamentals of PHP, including its syntax, constructs, and how to embed PHP within HTML. It covers essential topics such as PHP variables, operators, control structures, and examples of PHP scripts. The document serves as a foundational guide for beginners to understand and write PHP code effectively.

Uploaded by

mc.mashmello
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Chapter 11 PHP.

The document provides an overview of the basic fundamentals of PHP, including its syntax, constructs, and how to embed PHP within HTML. It covers essential topics such as PHP variables, operators, control structures, and examples of PHP scripts. The document serves as a foundational guide for beginners to understand and write PHP code effectively.

Uploaded by

mc.mashmello
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

THE BASIC

FUNDAMENTALS OF PHP
CONSTRUCTS AND SYNTAX
Contents
􀀵 Understand that PHP can be embedded within XHTML or HTML documents.
􀀵 Understand what tags denote the start and end of PHP code.
􀀵 Understand how it is possible to jump in and out of PHP within a document.
􀀵 Understand what the PHP parser does when it interprets a documen t
􀀵 Understand how to use the echo construct.
􀀵 Understand how each PHP instruction should be separated.
􀀵 How and why you should comment your PHP scripts.
􀀵 Understand the different PHP types that are supported.
􀀵 Understand what are and how to use PHP Variables.
􀀵 Understand what a PHP constant is and how to use them.
􀀵 Understand what an expression is and the role of operators and operands.
􀀵 Be aware of the different operators that PHP has available.
introduction
• PHP is a server scripting language, and a powerful tool for
making dynamic and interactive Web pages
• PHP is an acronym for "PHP: Hypertext Preprocessor" PHP is
a widely-used, open source scripting language
• PHP scripts are executed on the server.
What is a PHP File?
• PHP files can contain text, HTML, CSS, JavaScript, and PHP
code
• PHP code are executed on the server, and the result is returned
to the browser as plain HTML
• PHP files have extension ".php"
Your first PHP Script
<html>
<head>
<title>PHP Script</title>
</head>
<body>
<?php
echo "<p>Hello, World</p>";
?>
</body>
</html>
• Type the above script into your chosen editor and save
this in your chosen Web server directory. You should
save the PHP script file as example1-1.php.
• If we remove the full XHTML elements then our PHP
script looks like this:
<?php
echo ‘<p>Hello, World</p>’;
?>
Example 2
• <!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>
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
?>
• The default file extension for PHP files is ".php".
• A PHP file normally contains HTML tags, and some PHP
scripting code.
• The PHP code is enclosed within special start and end
tags which denote the start and end of the PHP script,
these tags are:
<?php
?>
• In this example the PHP code itself consists of a single
instruction which displays the text “Hello, World” on the
Web browser:
• PHP requires instructions to be terminated with a
semicolon at the end of each statement. example of a
PHP script with two instructions:
<?php
echo '<p>Hello, World!</p>';
echo '<p>Hello, World!</p>';
?>
Commenting Your Scripts
• Comments are human readable text which is ignored by the
PHP interpreter but are included to provide useful reminders
or instructions to the developer or future code reader. PHP
supports a number of different styles of comments:
• // this is a single line comment
• To shell script single style comments, which look like this:
• # this is a shell style comment
• And also multi-line comments, like this:
• /* this comment is an example of
• a multi-line comment */
PHP Case Sensitivity
• In PHP, all 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
legal (and equal):
• However; all variable names are case-sensitive.
Example
<!DOCTYPE html>
<html>
<body>

<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>

</body>
</html>
Basic Variables
• PHP variable starts with the $ sign, followed by the name of
the variable. Here is an example variable:
• $name = ‘Simon’;
• The variable is called “name” and it has been assigned the
value ‘Simon’.
• PHP variable names are case sensitive. Therefore the variable
$name is a different variable to $Name. Be careful!
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
naming your variables
• A valid variable name can start with a letter or an underscore
character, followed by any number of letters, numbers or
underscores.
• Valid variables
– <?php
– $var = 'Elizabeth';
– $_var = 56;
– $Var = 'Hall';
– echo “<p>$var $Var $_var</p>”;
– ?>
• invalid:
– var = ‘Simon’; // Invalid - does not start with a $ symbol
– $2var = ‘Simon’; // Invalid – name starts with a number.
Output Variables
• Example
<?php
$name = “JAMES";
echo “My name is : " . $name. "!";
?>
• Example 2 .
<!DOCTYPE >
<html >
<head>
<title>PHP Script</title>
</head>
<body>
<?php
$name=“STEPHEN";
echo "<p>Hello, World</p>";
echo "MY NAME IS ".$name;
?>
</body>
</html>
Operators and Operands
• An operator is something which when given a value will
produce another value. The operator is said to operate
upon the first value, which is where it gets its name.
Operands are the things which operators operate upon.
PHP has three different types of operators: unary
operators which only operate on a single value, binary
operators which operate on two values (these are the
latest group of operators which PHP supports) and finally
ternary operators which require three values. We shall
examine each of the operators supported in PHP.
Arithmetic Operators
<?php
Example
$intA = 5;
$intB = 4;
$intC = $intA + $intB;
echo "<p>$intA + $intB = $intC</p>";
$intC = $intA - $intB;
echo "<p>$intA - $intB = $intC</p>";
$intC = $intA * $intB;
echo "<p>$intA * $intB = $intC</p>";
$intC = $intA / $intB;
echo "<p>$intA / $intB = $intC</p>";
$intC = -$intA;
echo "<p>$intA minus = $intC</p>";
$intC = $intA % $intB;
echo "<p>$intA % $intB = $intC</p>";
?>
CONTROL STRUCTURES
• The if construct
if ( expression )
statement to be executed if condition is true

<?php
// File: if.php
$intA = 5;
$intB = 3;
if ($intA > $intB)
echo "<p>$intA is greater than $intB</p>";
?>
The if else construct
if ( expression )
statement to be executed if condition is true
else
statement to be executed if condition is false
<?php
// File: IFELSEPHP.php
$intA = 3;
$intB = 5;
if ($intA > $intB)
echo "<p>$intA is greater than $intB</p>";
else
echo "<p>$intA is less than or equal to $intB</p>";
?>
The if elseif construct
if ( expression )
statement to be executed if condition is true
elseif ( expression )
statement to be executed if elseif condition is true
else
<?php
// File: ELSEIF.php
$intNumber1 = 100;
$intNumber2 = 800;
if ($intNumber1 > $intNumber2) {
echo "<p>$intNumber1 is larger than $intNumber2</p>";
}
elseif ($intNumber1 == $intNumber2) {
echo "<p>$intNumber1 is equal to $intNumber2</p>";
}
else {
echo "<p>$intNumber1 is smaller than $intNumber2</p>";
}
?>
switch construct
switch ( expression ) {
case constant expression : statement
case constant expression : statement
...
default : statement
}
<?php TRY WITHOUT BREAK
// File: switch.php
$strName = "Elizabeth";
switch ($strName) {
case "Simon":
echo "<p>Hello Simon</p>";
break;
case "Elizabeth":
echo "<p>Hello Elizabeth</p>";
break;
case "Hayley":
echo "<p>Hello Hayley</p>";
break;
case "Alan":
echo "<p>Hello Alan</p>";
}
?>
while loops
while ( expression ) {
statement

}
<?php
// File: while1.php
$intCount = 1;
while ($intCount <= 10) {
echo "<p>Iteration $intCount</p>";
$intCount++;
}
?>
.
<?php
// File: while2.php
$intCount = 1;
while ($intCount <= 10) {
$intProd=$intCount *5;
echo "<p> $intCount X 5 = $intProd</p>";
$intCount++;
}
?>
Do while loops
do
statement
while ( expression );
<?php
// File: DOWHILE.php
$intCount = 1;
do {
$intProd=$intCount *5;
echo "<p> $intCount X 5 = $intProd</p>";
$intCount++;
} while ($intCount <= 10);
?>
For loop
for (expression; expression; expression)
statement

<?php
// File: For.php
for($intCount = 1; $intCount <= 10; $intCount++)
{
$intProd=$intCount *10;
echo "<p> $intCount X 10 = $intProd</p>";
}
?>
END

You might also like