SlideShare a Scribd company logo
iFour ConsultancyPHP
HYPERTEXT PREPROCESSOR
web Engineering ||
winter 2017
wahidullah Mudaser
assignment.cs2017@gmail.com
 Lecture 05
 Introduction To PHP
 Three-tiered website
 Introduction to PHP
 Server side scripting langauge
INDEX
http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India
Three-tiered Web Site
Client
User-agent: Firefox
Server
Apache HTTP Server
example request
GET / HTTP/1.1
Host: www.tamk.fi
User-Agent: Mozilla/5.0 (Mac..)
...
response
Database
MySQL
PHP
Server Side Scripting Language
 A “script” is a collection of program or sequence of instructions that is interpreted or carried out
by another program rather than by the computer processor.
 Client-side
 Server-side
 In server-side scripting, (such as PHP, ASP) the script is processed by the server Like: Apache,
ColdFusion, ISAPI and Microsoft's IIS on Windows.
 Client-side scripting such as JavaScript runs on the web browser.
Advantages of Server side Scripting Language
 Dynamic content.
 Computational capability.
 Database and file system access.
 Network access (from the server only).
 Built-in libraries and functions.
 Known platform for execution (as opposed to client-side,
 where the platform is uncontrolled.)
 Security improvements
What Is PHP?
 PHP is a server scripting language, and a powerful tool for making dynamic and interactive
Web pages.
 PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's
ASP.
 PHP is an acronym for "PHP: Hypertext Preprocessor"
 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use
Cont…
 PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS, etc.)
 PHP supports a wide range of databases
 PHP is free. Download it from the official PHP resource: www.php.net
 PHP is easy to learn and runs efficiently on the server side
Brief History of PHP
PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server-
side form generation in Unix.
PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables,
arrays, recursive functions, conditionals, iteration, regular expressions, etc.
PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by
Zeev Suraski and Andi Gutmans .
PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many
security features were added.
PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for
interoperability with Web Services, SQLite has been bundled with PHP
What is PHP File
PHP files have a file extension of ".php", ".php3", or ".phtml“
 PHP files can contain text, HTML tags and scripts
 PHP files are returned to the browser as plain HTML
Basic PHP Syntax
 A PHP script can be placed anywhere in the document.
 A PHP script starts with <?php and ends with ?>
 <!DOCTYPE html>
<html>
<body>
<h1>PHP TUTORIAL</h1>
<?php
echo “YCCE NAGPUR”;
?>
</body>
</html>
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)
Basic PHP Syntax
<!DOCTYPE html>
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
</body>
</html>
PHP is Loosely Typed language
 In the example above, notice that we did not have to tell
PHP which data type the variable is.
 PHP automatically converts the variable to the correct
data type, depending on its value.
 In other languages such as C, C++, and Java, the
programmer must declare the name and type of the
variable before using it.
PHP Variables Scope
 In PHP, variables can be declared anywhere in the script.
 The scope of a variable is the part of the script where the
variable can be referenced/used.
 PHP has three different variable scopes:
 local
 global
 static
PHP global keyword
 The global keyword is used to access a global variable from within a
function.
 To do this, use the global keyword before the variables
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest();
echo $y;
?>
PHP Static Keyword
 <?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
PHP echo Statement
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple
parameters.";
?>
PHP print statement
<?php
$txt1 = "Learn PHP";
$txt2 = “ycce.com";
$x = 5;
$y = 4;
print "<h2>$txt1</h2>";
print "Study PHP at $txt2<br>";
print $x + $y;
?>
Echo V.S Print
 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.
PHP Data types
 PHP supports the following data types:
 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 Object
 NULL
 Resource
Data types cont..
PHP String:
 A string is a sequence of characters, like "Hello world!".
 A string can be any text inside quotes. You can use single
or double quotes:
Example
<?php
$x = 5985;
var_dump($x);
?>
Cont…
PHP Array:
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP Object
 An object is a data type which stores data and information on
how to process that data.
 In PHP, an object must be explicitly declared.
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
// show object properties
echo $herbie->model;
?>
PHP NULL value
 Null is a special data type which can have only one value:
NULL.
 A variable of data type NULL is a variable that has no
value assigned to it.
 Note: If a variable is created without a value, it is
automatically assigned a value of NULL.
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
PHP String Functions
Get The Length of a String:
 The PHP strlen() function returns the length of a string
(number of characters).
 The example below returns the length of the string "Hello
world!":
Example
<?php
echo strlen("Hello world!"); // outputs 12
?>
Cont…
Count The Number of Words in a String:
Example
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
Reverse a String:
Example
<?php
echo strrev("Hello world!");
?>
Cont…
Search For a Specific Text Within a String:
 If a match is found, the function returns the character
position of the first match. If no match is found, it will
return FALSE.
Example
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
Cont…
 Replace Text Within a String:
Example
<?php
echo str_replace("world", "Dolly", "Hello world!"); //
outputs Hello Dolly!
?>
PHP Constants
 A constant is an identifier (name) for a simple value. The value cannot
be changed during the script.
 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.
PHP Constants
Syntax:
define(name, value, case-insensitive)
Parameters:
 name: Specifies the name of the constant
 value: Specifies the value of the constant
 case-insensitive: Specifies whether the constant name should be case-insensitive.
Default is false
Example - Constants
<?php
define(“NAME", "Welcome Ahmadullah!");
echo NAME;
?>
Constants are Global:
<?php
define(“NAME", "Welcome Ahmadullah!");
function myTest() {
echo NAME;
}
myTest();
?>
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
PHP Operators
PHP Arithmetic Operators:
PHP Operators
PHP Assignment Operators:
PHP Operators
PHP Comparison Operators:
PHP Operators
PHP Increment / Decrement Operators:
PHP Operators
PHP Logical Operators:
PHP Operators
PHP String Operators:
PHP Operators
PHP Array Operators:
PHP Conditional Statements
 In PHP we have the following conditional statements:
 if statement - executes some code only if a specified
condition is true
 if...else statement - executes some code if a condition is
true and another code if the condition is false
 if...elseif....else statement - specifies a new condition to
test, if the first condition is false
 switch statement - selects one of many blocks of code to
be executed
PHP – The IF Statement
 The if statement is used to execute some code only if a
specified condition is true.
Syntax
if (condition)
{
code to be executed if condition is true;
}
PHP - The if...else Statement
 Use the if....else statement to execute some code if a
condition is true and another code if the condition is
false.
Syntax:
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP - The if...elseif....else Statement
 Use the if....elseif...else statement to specify a new
condition to test, if the first condition is false.
Syntax:
if (condition) {
code to be executed if condition is true;
} elseif (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
PHP - The switch Statement
 Use the switch statement to select one of many blocks of
code to be executed.
Syntax:
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
PHP Loops
In PHP, we have the following looping statements:
 while - loops through a block of code as long as the
specified condition is true
 do...while - loops through a block of code once, and then
repeats the loop as long as the specified condition is true
 for - loops through a block of code a specified number of
times
 foreach - loops through a block of code for each element
in an array
While Loops
 The while loop executes a block of code as long as the
specified condition is true.
Syntax:
while (condition is true)
{
code to be executed;
}
Do…while loop
 The do...while loop will always execute the block of code
once, it will then check the condition, and repeat the loop
while the specified condition is true.
Syntax:
do {
code to be executed;
}
while (condition is true);
For Loop
 The for loop is used when you know in advance how
many times the script should run.
Syntax:
for (init counter; test counter; increment counter)
{
code to be executed;
}
Foreach Loop
 The foreach loop works only on arrays, and is used to
loop through each key/value pair in an array.
Syntax:
foreach ($array as $value)
{
code to be executed;
}
Example – foreach loop
 For every loop iteration, the value of the current array
element is assigned to $value and the array pointer is
moved by one, until it reaches the last array element.
Example:
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
PHP Functions
PHP User Defined Functions
 Besides the built-in PHP functions, we can create our own
functions.
 A function is a block of statements that can be used
repeatedly in a program.
 A function will not execute immediately when a page
loads.
 A function will be executed by a call to the function.
Questions?
http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India

More Related Content

PPTX
PDF
Chap 4 PHP.pdf
PPT
Introduction To PHP
PPT
Oops concepts in php
PDF
Php introduction
PPTX
Php.ppt
PPTX
Java script
Chap 4 PHP.pdf
Introduction To PHP
Oops concepts in php
Php introduction
Php.ppt
Java script

What's hot (20)

PPTX
PPSX
Php and MySQL
PPT
PHP - Introduction to PHP AJAX
PPT
React js
PPT
Php with MYSQL Database
PPTX
Javascript 101
PPTX
Html5 and-css3-overview
PPTX
JavaScript
PPTX
PHP slides
PDF
Express node js
PDF
3. Java Script
PPT
Php forms
PDF
TypeScript - An Introduction
PDF
TypeScript Introduction
PPTX
Introducing type script
PPT
Java script final presentation
PPT
Ajax Ppt
PDF
The New JavaScript: ES6
PDF
Workshop 4: NodeJS. Express Framework & MongoDB.
PPT
Control Structures In Php 2
Php and MySQL
PHP - Introduction to PHP AJAX
React js
Php with MYSQL Database
Javascript 101
Html5 and-css3-overview
JavaScript
PHP slides
Express node js
3. Java Script
Php forms
TypeScript - An Introduction
TypeScript Introduction
Introducing type script
Java script final presentation
Ajax Ppt
The New JavaScript: ES6
Workshop 4: NodeJS. Express Framework & MongoDB.
Control Structures In Php 2
Ad

Similar to Introduction to PHP - Basics of PHP (20)

PDF
WT_PHP_PART1.pdf
PPTX
Php intro by sami kz
PPTX
php Chapter 1.pptx
PDF
Web Development Course: PHP lecture 1
PPTX
PHP Lecture 01 .pptx PHP Lecture 01 pptx
PDF
Introduction of PHP.pdf
PPTX
Php Tutorial
PDF
Programming in PHP Course Material BCA 6th Semester
PPTX
PHP2An introduction to Gnome.pptx.j.pptx
PDF
PHP Basic & Variables
PPTX
The basics of php for engeneering students
PPTX
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
PDF
basic concept of php(Gunikhan sonowal)
PPTX
introduction to php and its uses in daily
PPTX
Php by shivitomer
PDF
Wt unit 4 server side technology-2
PDF
1336333055 php tutorial_from_beginner_to_master
PPT
WT_PHP_PART1.pdf
Php intro by sami kz
php Chapter 1.pptx
Web Development Course: PHP lecture 1
PHP Lecture 01 .pptx PHP Lecture 01 pptx
Introduction of PHP.pdf
Php Tutorial
Programming in PHP Course Material BCA 6th Semester
PHP2An introduction to Gnome.pptx.j.pptx
PHP Basic & Variables
The basics of php for engeneering students
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
basic concept of php(Gunikhan sonowal)
introduction to php and its uses in daily
Php by shivitomer
Wt unit 4 server side technology-2
1336333055 php tutorial_from_beginner_to_master
Ad

More from wahidullah mudaser (6)

PDF
AJAX-Asynchronous JavaScript and XML
PDF
XML - Extensive Markup Language
PDF
PHP file handling
PDF
Object Oriented Programming in PHP
PDF
PHP with MySQL
PDF
PHP Arrays - indexed and associative array.
AJAX-Asynchronous JavaScript and XML
XML - Extensive Markup Language
PHP file handling
Object Oriented Programming in PHP
PHP with MySQL
PHP Arrays - indexed and associative array.

Recently uploaded (20)

PDF
Spectral efficient network and resource selection model in 5G networks
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
KodekX | Application Modernization Development
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Spectroscopy.pptx food analysis technology
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Approach and Philosophy of On baking technology
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Machine learning based COVID-19 study performance prediction
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectral efficient network and resource selection model in 5G networks
NewMind AI Weekly Chronicles - August'25 Week I
KodekX | Application Modernization Development
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Spectroscopy.pptx food analysis technology
The Rise and Fall of 3GPP – Time for a Sabbatical?
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
MIND Revenue Release Quarter 2 2025 Press Release
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Approach and Philosophy of On baking technology
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Machine learning based COVID-19 study performance prediction
Diabetes mellitus diagnosis method based random forest with bat algorithm
Unlocking AI with Model Context Protocol (MCP)
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
“AI and Expert System Decision Support & Business Intelligence Systems”

Introduction to PHP - Basics of PHP

  • 1. iFour ConsultancyPHP HYPERTEXT PREPROCESSOR web Engineering || winter 2017 wahidullah Mudaser assignment.cs2017@gmail.com  Lecture 05  Introduction To PHP
  • 2.  Three-tiered website  Introduction to PHP  Server side scripting langauge INDEX http://www.ifourtechnolab.com/ ASP.NET Software Development Companies India
  • 3. Three-tiered Web Site Client User-agent: Firefox Server Apache HTTP Server example request GET / HTTP/1.1 Host: www.tamk.fi User-Agent: Mozilla/5.0 (Mac..) ... response Database MySQL PHP
  • 4. Server Side Scripting Language  A “script” is a collection of program or sequence of instructions that is interpreted or carried out by another program rather than by the computer processor.  Client-side  Server-side  In server-side scripting, (such as PHP, ASP) the script is processed by the server Like: Apache, ColdFusion, ISAPI and Microsoft's IIS on Windows.  Client-side scripting such as JavaScript runs on the web browser.
  • 5. Advantages of Server side Scripting Language  Dynamic content.  Computational capability.  Database and file system access.  Network access (from the server only).  Built-in libraries and functions.  Known platform for execution (as opposed to client-side,  where the platform is uncontrolled.)  Security improvements
  • 6. What Is PHP?  PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.  PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft's ASP.  PHP is an acronym for "PHP: Hypertext Preprocessor"  PHP is a widely-used, open source scripting language  PHP scripts are executed on the server  PHP is free to download and use
  • 7. Cont…  PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)  PHP is compatible with almost all servers used today (Apache, IIS, etc.)  PHP supports a wide range of databases  PHP is free. Download it from the official PHP resource: www.php.net  PHP is easy to learn and runs efficiently on the server side
  • 8. Brief History of PHP PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994. It was initially developed for HTTP usage logging and server- side form generation in Unix. PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP
  • 9. What is PHP File PHP files have a file extension of ".php", ".php3", or ".phtml“  PHP files can contain text, HTML tags and scripts  PHP files are returned to the browser as plain HTML
  • 10. Basic PHP Syntax  A PHP script can be placed anywhere in the document.  A PHP script starts with <?php and ends with ?>  <!DOCTYPE html> <html> <body> <h1>PHP TUTORIAL</h1> <?php echo “YCCE NAGPUR”; ?> </body> </html>
  • 11. 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)
  • 12. Basic PHP Syntax <!DOCTYPE html> <html> <body> <?php // This is a single-line comment # This is also a single-line comment /* This is a multiple-lines comment block that spans over multiple lines */ // You can also use comments to leave out parts of a code line $x = 5 /* + 15 */ + 5; echo $x; ?> </body> </html>
  • 13. PHP is Loosely Typed language  In the example above, notice that we did not have to tell PHP which data type the variable is.  PHP automatically converts the variable to the correct data type, depending on its value.  In other languages such as C, C++, and Java, the programmer must declare the name and type of the variable before using it.
  • 14. PHP Variables Scope  In PHP, variables can be declared anywhere in the script.  The scope of a variable is the part of the script where the variable can be referenced/used.  PHP has three different variable scopes:  local  global  static
  • 15. PHP global keyword  The global keyword is used to access a global variable from within a function.  To do this, use the global keyword before the variables <?php $x = 5; $y = 10; function myTest() { global $x, $y; $y = $x + $y; } myTest(); echo $y; ?>
  • 16. PHP Static Keyword  <?php function myTest() { static $x = 0; echo $x; $x++; } myTest(); myTest(); myTest(); ?>
  • 17. PHP echo Statement <?php echo "<h2>PHP is Fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This ", "string ", "was ", "made ", "with multiple parameters."; ?>
  • 18. PHP print statement <?php $txt1 = "Learn PHP"; $txt2 = “ycce.com"; $x = 5; $y = 4; print "<h2>$txt1</h2>"; print "Study PHP at $txt2<br>"; print $x + $y; ?> Echo V.S Print  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.
  • 19. PHP Data types  PHP supports the following data types:  String  Integer  Float (floating point numbers - also called double)  Boolean  Array  Object  NULL  Resource
  • 20. Data types cont.. PHP String:  A string is a sequence of characters, like "Hello world!".  A string can be any text inside quotes. You can use single or double quotes: Example <?php $x = 5985; var_dump($x); ?>
  • 21. Cont… PHP Array: <?php $cars = array("Volvo","BMW","Toyota"); var_dump($cars); ?>
  • 22. PHP Object  An object is a data type which stores data and information on how to process that data.  In PHP, an object must be explicitly declared. <?php class Car { function Car() { $this->model = "VW"; } } // create an object $herbie = new Car(); // show object properties echo $herbie->model; ?>
  • 23. PHP NULL value  Null is a special data type which can have only one value: NULL.  A variable of data type NULL is a variable that has no value assigned to it.  Note: If a variable is created without a value, it is automatically assigned a value of NULL. Example <?php $x = "Hello world!"; $x = null; var_dump($x); ?>
  • 24. PHP String Functions Get The Length of a String:  The PHP strlen() function returns the length of a string (number of characters).  The example below returns the length of the string "Hello world!": Example <?php echo strlen("Hello world!"); // outputs 12 ?>
  • 25. Cont… Count The Number of Words in a String: Example <?php echo str_word_count("Hello world!"); // outputs 2 ?> Reverse a String: Example <?php echo strrev("Hello world!"); ?>
  • 26. Cont… Search For a Specific Text Within a String:  If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE. Example <?php echo strpos("Hello world!", "world"); // outputs 6 ?>
  • 27. Cont…  Replace Text Within a String: Example <?php echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly! ?>
  • 28. PHP Constants  A constant is an identifier (name) for a simple value. The value cannot be changed during the script.  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.
  • 29. PHP Constants Syntax: define(name, value, case-insensitive) Parameters:  name: Specifies the name of the constant  value: Specifies the value of the constant  case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false
  • 30. Example - Constants <?php define(“NAME", "Welcome Ahmadullah!"); echo NAME; ?> Constants are Global: <?php define(“NAME", "Welcome Ahmadullah!"); function myTest() { echo NAME; } myTest(); ?>
  • 31. 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
  • 35. PHP Operators PHP Increment / Decrement Operators:
  • 39. PHP Conditional Statements  In PHP we have the following conditional statements:  if statement - executes some code only if a specified condition is true  if...else statement - executes some code if a condition is true and another code if the condition is false  if...elseif....else statement - specifies a new condition to test, if the first condition is false  switch statement - selects one of many blocks of code to be executed
  • 40. PHP – The IF Statement  The if statement is used to execute some code only if a specified condition is true. Syntax if (condition) { code to be executed if condition is true; }
  • 41. PHP - The if...else Statement  Use the if....else statement to execute some code if a condition is true and another code if the condition is false. Syntax: if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 42. PHP - The if...elseif....else Statement  Use the if....elseif...else statement to specify a new condition to test, if the first condition is false. Syntax: if (condition) { code to be executed if condition is true; } elseif (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
  • 43. PHP - The switch Statement  Use the switch statement to select one of many blocks of code to be executed. Syntax: switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; case label3: code to be executed if n=label3; break; ... default: code to be executed if n is different from all labels; }
  • 44. PHP Loops In PHP, we have the following looping statements:  while - loops through a block of code as long as the specified condition is true  do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true  for - loops through a block of code a specified number of times  foreach - loops through a block of code for each element in an array
  • 45. While Loops  The while loop executes a block of code as long as the specified condition is true. Syntax: while (condition is true) { code to be executed; }
  • 46. Do…while loop  The do...while loop will always execute the block of code once, it will then check the condition, and repeat the loop while the specified condition is true. Syntax: do { code to be executed; } while (condition is true);
  • 47. For Loop  The for loop is used when you know in advance how many times the script should run. Syntax: for (init counter; test counter; increment counter) { code to be executed; }
  • 48. Foreach Loop  The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax: foreach ($array as $value) { code to be executed; }
  • 49. Example – foreach loop  For every loop iteration, the value of the current array element is assigned to $value and the array pointer is moved by one, until it reaches the last array element. Example: <?php $colors = array("red", "green", "blue", "yellow"); foreach ($colors as $value) { echo "$value <br>"; } ?>
  • 50. PHP Functions PHP User Defined Functions  Besides the built-in PHP functions, we can create our own functions.  A function is a block of statements that can be used repeatedly in a program.  A function will not execute immediately when a page loads.  A function will be executed by a call to the function.