SlideShare ist ein Scribd-Unternehmen logo
1 von 34
BIT 2105
WEB DESIGN III
PHP BASICS
Variables and Data Types
• PHP provides support for six general data types:
• Integers
• Floating-point numbers
• Strings
• Arrays
• Objects
• Booleans
2
Variables
• A variable is a named memory location that contains
data that may be manipulated throughout the
execution of the program.
• A variable always begins with a dollar sign ($). The
following are all valid variables:
$color
$operating_system
$_some_variable
$model
3
Variables
• $sentence = "This is a sentence."; // $sentence
evaluates to string.
• $price = 42.99; // $price evaluates to a floating-point
• $weight = 185; // $weight evaluates to an integer.
• You can declare variables anywhere in a PHP script.
However, the location of the declaration greatly
influences the realm in which a variable can be
accessed. This access domain is known as its scope.
4
Variables
• $sentence = "This is a sentence."; // $sentence
evaluates to string.
• $price = 42.99; // $price evaluates to a floating-point
• $weight = 185; // $weight evaluates to an integer.
• You can declare variables anywhere in a PHP script.
However, the location of the declaration greatly
influences the realm in which a variable can be
accessed. This access domain is known as its scope.
5
Variable Scope
• Scope can be defined as the range of availability a
variable has to the program in which it is declared.
PHP variables can be one of four scope types:
• Local variables
• Function parameters
• Global variables
• Static variables
6
Variable Scope
• Scope can be defined as the range of availability a
variable has to the program in which it is declared.
PHP variables can be one of four scope types:
• Local variables
• Function parameters
• Global variables
• Static variables
7
Local Variables
• A variable declared in a function is considered local;
that is, it can be referenced solely in that function.
Any assignment outside of that function will be
considered to be an entirely different variable from
the one contained in the function.
$x = 4;
function assignx () {
$x = 0;
print "$x inside function is $x. <br>";
}
8
Function Parameters
• As is the case with many other programming
languages, in PHP any function that accepts
arguments must declare these arguments in the
function header.
// multiply a value by 10 and return it to the caller
function x10 ($value) {
$value = $value * 10;
return $value;
}
9
Global Variables
• In contrast to local variables, a global variable can be
accessed in any part of the program. However, in order
to be modified, a global variable must be explicitly
declared to be global in the function in which it is to be
modified.
$somevar = 15;
function addit() {
GLOBAL $somevar;
$somevar++;
print "Somevar is $somevar";
}
addit();
10
Static Variables
• In contrast to the variables declared as function
parameters, which are destroyed on the function's exit,
a static variable will not lose its value when the
function exits and will still hold that value should the
function be called again.
• You can declare a variable to be static simply by placing
the keyword STATIC in front of the variable name.
• STATIC $somevar;
11
Static Variables
• In contrast to the variables declared as function
parameters, which are destroyed on the function's exit,
a static variable will not lose its value when the
function exits and will still hold that value should the
function be called again.
• You can declare a variable to be static simply by placing
the keyword STATIC in front of the variable name.
• STATIC $somevar;
12
String Assignments
• Strings can be delimited in two ways, using either
double quotations ("") or single quotations ('').
• The following two string declarations produce the same
result:
• $food = "meatloaf";
• $food = 'meatloaf';
• However, the following two declarations result in two
drastically different outcomes:
• $sentence = "My favourite food is $food";
• $sentence2 = 'My favourite food is $food';
13
String Assignments
• The following string is what exactly will be assigned to
$sentence. Notice how the variable $food is
automatically interpreted:
My favourite food is meatloaf.
• Whereas $sentence2 will be assigned the string as
follows:
My favourite food is $food.
• These differing outcomes are due to the usage of double and single quotation
marks in assigning the corresponding strings to $sentence and $sentence2.
14
Supported String Delimiters
SEQUENCE REPRESENTATION
n Newline
r Carriage return
t Horizontal tab
 Backslash
$ Dollar sign
" Double-quotation mark
15
Use of String Delimiters
• Double-quoted string recognizes all available
delimiters, a single-quoted string recognizes only
the delimiters "" and "".
• Consider an example of the contrasting outcomes
when assigning strings enclosed in double and
single quotation marks:
$double_list = "item1nitem2nitem3";
$single_list = 'item1nitem2nitem3';
16
Out put
• If you print both strings to the browser, the double-
quoted string will conceal the newline character, but
the single-quoted string will print it just as if it were any
other character.
• Although many of the delimited characters will be
irrelevant in the browser, this will prove to be a major
factor when formatting for various other media.
• Keep this difference in mind when using double- or
single-quoted enclosures so as to ensure the intended
outcome.
17
Arrays
• An array is a list of elements each having the same
type.
• There are two types of arrays:
a) those that are accessed in accordance with the index
position in which the element resides
b) those that are associative in nature, accessed by a key
value that bears some sort of association with its
corresponding value.
18
Single-Dimension Indexed Arrays
• The general syntax of a single-dimension array is:
$name[index1];
• A single-dimension array can be created as follows:
$meat[0] = "chicken";
$meat[1] = "steak";
$meat[2] = "turkey";
• If you execute this command:
print $meat[1];
• The following will be output to the browser:
steak
19
Single-Dimension Indexed Arrays
• Alternatively, arrays may be created using PHP's array()
function. You can use this function to create the same
$meat array as the one in the preceding example:
$meat = array("chicken", "steak", "turkey");
• Executing the same print command yields the same
results as in the previous example, again producing
"steak".
20
Single-Dimension Indexed Arrays
• You can also assign values to the end of the array
simply by assigning values to an array variable using
empty brackets.
• Therefore, another way to assign values to the $meat
array is as follows:
$meat[] = "chicken";
$meat[] = "steak";
$meat[] = "turkey";
21
Single-Dimension Associative Arrays
• Associative arrays are particularly convenient when it
makes more sense to map an array using words rather
than integers.
$pairings["zinfandel"] = "Broiled Veal Chops";
$pairings["merlot"] = "Baked Ham";
$pairings["sauvignon"] = "Prime Rib";
$pairings["sauternes"] = "Roasted Salmon";
22
Single-Dimension Associative Arrays
• An alternative method in which to create an array is via
PHP's array() function:
$pairings = array(
zinfandel => "Broiled Veal Chops",
merlot => "Baked Ham",
sauvignon => "Prime Rib",
sauternes => "Roasted Salmon";
);
• This assignment method bears no difference in functionality
from the previous, other than the format in which it was
created.
23
Multidimensional Indexed Arrays
• Multidimensional indexed arrays function much like
their single-dimension counterparts, except that more
than one index array is used to specify an element.
• There is no limit as to the dimension size. The general
syntax of a multidimensional array is:
$name[index1] [index2]..[indexN];
• An element of a two-dimensional indexed array could
be referenced as follows:
$position = $chess_board[5][4];
24
PHP's Operators
OPERATOR PURPOSE
++ — Autoincrement, autodecrement
/ * % Division, multiplication, modulus
+ − . Addition, subtraction, concatenation
< <= Less than, less than or equal to,
> >= Greater than, greater than or equal to
== != Is equal to, is not equal to
=== <> identical to, is not equal to
&& || Boolean AND, boolean OR
25
Assignment Operators
EXAMPLE OUTCOME
$a = 5; $a equals 5
$a += 5; $a equals $a plus 5
$a *= 5; $a equals $a multiplied by 5
$a /= 5; $a equals $a divided by 5
$a .= 5; $a equals $a concatenated with 5
26
Conditional statements
• Conditional statements are used to perform different
actions based on different conditions.
▫ if statement - use this statement to execute some code
only if a specified condition is true
▫ if...else statement - use this statement to execute some
code if a condition is true and another code if the condition
is false
▫ if...elseif....else statement - use this statement to select
one of several blocks of code to be executed
▫ switch statement - use this statement to select one of
many blocks of code to be executed
27
Conditional statements
• The if statement - use this statement to
execute some code only if a specified
condition is true
<html>
<body>
<?php
$p=date("D");
if ($p==“Mon”) echo “Today is Monday!";
?>
</body>
</html>
28
• The if...else Statement
• Use the if....else statement to execute some code if
a condition is true and another code if a condition is
false.
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
Conditional statements
29
• The if...else Statement
<html>
<body>
<?php
$p=date(“D”);
if ($p==“Mon") {
echo “Welcome!<br />”;
echo “Today is Monday!”;
}
else
echo “Have a nice day!”;
?>
</body>
</html>
Conditional statements
30
• The if...elseif....else Statement
• Use the if....elseif...else statement to select one of
several blocks of code to be executed.
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;
Conditional statements
31
<html>
<body>
<?php
$p=date(“D”);
if ($p==“Mon")
echo “Today is Monday!”;
elseif ($p==“Thu")
echo “Today is Thursday!”;
else
echo “Have a nice day!”;
?>
</body>
</html>
Conditional statements
32
• The PHP Switch Statement
• Use the switch statement to select one of many
blocks of code to be executed.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1
and label2;
}
Conditional statements
33
<html> <body>
<?php
switch ($y)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
default:
echo "No number between 1 and 2";
}
?>
</body> </html>
Conditional statements
34

Weitere ähnliche Inhalte

Was ist angesagt?

CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
brian d foy
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
FrankRodenbaugh
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13
Rafael Dohms
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
Magecom Ukraine
 

Was ist angesagt? (20)

Forget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers CracowForget about Index.php and build you applications around HTTP - PHPers Cracow
Forget about Index.php and build you applications around HTTP - PHPers Cracow
 
November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2November Camp - Spec BDD with PHPSpec 2
November Camp - Spec BDD with PHPSpec 2
 
Advanced php testing in action
Advanced php testing in actionAdvanced php testing in action
Advanced php testing in action
 
CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)CGI::Prototype (NPW 2006)
CGI::Prototype (NPW 2006)
 
Perl Programming - 02 Regular Expression
Perl Programming - 02 Regular ExpressionPerl Programming - 02 Regular Expression
Perl Programming - 02 Regular Expression
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Frank Rodenbaugh Portfolio
Frank Rodenbaugh PortfolioFrank Rodenbaugh Portfolio
Frank Rodenbaugh Portfolio
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016
 
PHP 8.1 - What's new and changed
PHP 8.1 - What's new and changedPHP 8.1 - What's new and changed
PHP 8.1 - What's new and changed
 
Framework
FrameworkFramework
Framework
 
Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!Forget about index.php and build you applications around HTTP!
Forget about index.php and build you applications around HTTP!
 
Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13Your code sucks, let's fix it! - php|tek13
Your code sucks, let's fix it! - php|tek13
 
The IoC Hydra
The IoC HydraThe IoC Hydra
The IoC Hydra
 
Система рендеринга в Magento
Система рендеринга в MagentoСистема рендеринга в Magento
Система рендеринга в Magento
 
PHP variables
PHP  variablesPHP  variables
PHP variables
 
TDD with PhpSpec
TDD with PhpSpecTDD with PhpSpec
TDD with PhpSpec
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
Staying railsy - while scaling complexity or Ruby on Rails in Enterprise Soft...
 

Ähnlich wie Lecture4 php by okello erick

MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
BUDNET
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
pooja bhandari
 

Ähnlich wie Lecture4 php by okello erick (20)

Zend Certification Preparation Tutorial
Zend Certification Preparation TutorialZend Certification Preparation Tutorial
Zend Certification Preparation Tutorial
 
Lecture3 php by okello erick
Lecture3 php by okello erickLecture3 php by okello erick
Lecture3 php by okello erick
 
MIND sweeping introduction to PHP
MIND sweeping introduction to PHPMIND sweeping introduction to PHP
MIND sweeping introduction to PHP
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
05php
05php05php
05php
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
PHP Basic
PHP BasicPHP Basic
PHP Basic
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
Php classes in mumbai
Php classes in mumbaiPhp classes in mumbai
Php classes in mumbai
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
05php
05php05php
05php
 
Php basics
Php basicsPhp basics
Php basics
 
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdfIT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
IT2255 Web Essentials - Unit IV Server-Side Processing and Scripting - PHP.pdf
 
Introduction to php basics
Introduction to php   basicsIntroduction to php   basics
Introduction to php basics
 
PHP - Introduction to PHP
PHP -  Introduction to PHPPHP -  Introduction to PHP
PHP - Introduction to PHP
 
Practical approach to perl day1
Practical approach to perl day1Practical approach to perl day1
Practical approach to perl day1
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikul
 

Mehr von okelloerick (10)

My sql statements by okello erick
My sql statements by okello erickMy sql statements by okello erick
My sql statements by okello erick
 
Lecture8 php page control by okello erick
Lecture8 php page control by okello erickLecture8 php page control by okello erick
Lecture8 php page control by okello erick
 
Lecture7 form processing by okello erick
Lecture7 form processing by okello erickLecture7 form processing by okello erick
Lecture7 form processing by okello erick
 
Lecture6 display data by okello erick
Lecture6 display data by okello erickLecture6 display data by okello erick
Lecture6 display data by okello erick
 
Lecture5 my sql statements by okello erick
Lecture5 my sql statements by okello erickLecture5 my sql statements by okello erick
Lecture5 my sql statements by okello erick
 
Lecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erickLecture3 mysql gui by okello erick
Lecture3 mysql gui by okello erick
 
Lecture2 mysql by okello erick
Lecture2 mysql by okello erickLecture2 mysql by okello erick
Lecture2 mysql by okello erick
 
Lecture1 introduction by okello erick
Lecture1 introduction by okello erickLecture1 introduction by okello erick
Lecture1 introduction by okello erick
 
Data commn intro by okello erick
Data commn intro by okello erickData commn intro by okello erick
Data commn intro by okello erick
 
Computer networks--networking hardware
Computer networks--networking hardwareComputer networks--networking hardware
Computer networks--networking hardware
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 

Lecture4 php by okello erick

  • 1. BIT 2105 WEB DESIGN III PHP BASICS
  • 2. Variables and Data Types • PHP provides support for six general data types: • Integers • Floating-point numbers • Strings • Arrays • Objects • Booleans 2
  • 3. Variables • A variable is a named memory location that contains data that may be manipulated throughout the execution of the program. • A variable always begins with a dollar sign ($). The following are all valid variables: $color $operating_system $_some_variable $model 3
  • 4. Variables • $sentence = "This is a sentence."; // $sentence evaluates to string. • $price = 42.99; // $price evaluates to a floating-point • $weight = 185; // $weight evaluates to an integer. • You can declare variables anywhere in a PHP script. However, the location of the declaration greatly influences the realm in which a variable can be accessed. This access domain is known as its scope. 4
  • 5. Variables • $sentence = "This is a sentence."; // $sentence evaluates to string. • $price = 42.99; // $price evaluates to a floating-point • $weight = 185; // $weight evaluates to an integer. • You can declare variables anywhere in a PHP script. However, the location of the declaration greatly influences the realm in which a variable can be accessed. This access domain is known as its scope. 5
  • 6. Variable Scope • Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types: • Local variables • Function parameters • Global variables • Static variables 6
  • 7. Variable Scope • Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types: • Local variables • Function parameters • Global variables • Static variables 7
  • 8. Local Variables • A variable declared in a function is considered local; that is, it can be referenced solely in that function. Any assignment outside of that function will be considered to be an entirely different variable from the one contained in the function. $x = 4; function assignx () { $x = 0; print "$x inside function is $x. <br>"; } 8
  • 9. Function Parameters • As is the case with many other programming languages, in PHP any function that accepts arguments must declare these arguments in the function header. // multiply a value by 10 and return it to the caller function x10 ($value) { $value = $value * 10; return $value; } 9
  • 10. Global Variables • In contrast to local variables, a global variable can be accessed in any part of the program. However, in order to be modified, a global variable must be explicitly declared to be global in the function in which it is to be modified. $somevar = 15; function addit() { GLOBAL $somevar; $somevar++; print "Somevar is $somevar"; } addit(); 10
  • 11. Static Variables • In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again. • You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name. • STATIC $somevar; 11
  • 12. Static Variables • In contrast to the variables declared as function parameters, which are destroyed on the function's exit, a static variable will not lose its value when the function exits and will still hold that value should the function be called again. • You can declare a variable to be static simply by placing the keyword STATIC in front of the variable name. • STATIC $somevar; 12
  • 13. String Assignments • Strings can be delimited in two ways, using either double quotations ("") or single quotations (''). • The following two string declarations produce the same result: • $food = "meatloaf"; • $food = 'meatloaf'; • However, the following two declarations result in two drastically different outcomes: • $sentence = "My favourite food is $food"; • $sentence2 = 'My favourite food is $food'; 13
  • 14. String Assignments • The following string is what exactly will be assigned to $sentence. Notice how the variable $food is automatically interpreted: My favourite food is meatloaf. • Whereas $sentence2 will be assigned the string as follows: My favourite food is $food. • These differing outcomes are due to the usage of double and single quotation marks in assigning the corresponding strings to $sentence and $sentence2. 14
  • 15. Supported String Delimiters SEQUENCE REPRESENTATION n Newline r Carriage return t Horizontal tab Backslash $ Dollar sign " Double-quotation mark 15
  • 16. Use of String Delimiters • Double-quoted string recognizes all available delimiters, a single-quoted string recognizes only the delimiters "" and "". • Consider an example of the contrasting outcomes when assigning strings enclosed in double and single quotation marks: $double_list = "item1nitem2nitem3"; $single_list = 'item1nitem2nitem3'; 16
  • 17. Out put • If you print both strings to the browser, the double- quoted string will conceal the newline character, but the single-quoted string will print it just as if it were any other character. • Although many of the delimited characters will be irrelevant in the browser, this will prove to be a major factor when formatting for various other media. • Keep this difference in mind when using double- or single-quoted enclosures so as to ensure the intended outcome. 17
  • 18. Arrays • An array is a list of elements each having the same type. • There are two types of arrays: a) those that are accessed in accordance with the index position in which the element resides b) those that are associative in nature, accessed by a key value that bears some sort of association with its corresponding value. 18
  • 19. Single-Dimension Indexed Arrays • The general syntax of a single-dimension array is: $name[index1]; • A single-dimension array can be created as follows: $meat[0] = "chicken"; $meat[1] = "steak"; $meat[2] = "turkey"; • If you execute this command: print $meat[1]; • The following will be output to the browser: steak 19
  • 20. Single-Dimension Indexed Arrays • Alternatively, arrays may be created using PHP's array() function. You can use this function to create the same $meat array as the one in the preceding example: $meat = array("chicken", "steak", "turkey"); • Executing the same print command yields the same results as in the previous example, again producing "steak". 20
  • 21. Single-Dimension Indexed Arrays • You can also assign values to the end of the array simply by assigning values to an array variable using empty brackets. • Therefore, another way to assign values to the $meat array is as follows: $meat[] = "chicken"; $meat[] = "steak"; $meat[] = "turkey"; 21
  • 22. Single-Dimension Associative Arrays • Associative arrays are particularly convenient when it makes more sense to map an array using words rather than integers. $pairings["zinfandel"] = "Broiled Veal Chops"; $pairings["merlot"] = "Baked Ham"; $pairings["sauvignon"] = "Prime Rib"; $pairings["sauternes"] = "Roasted Salmon"; 22
  • 23. Single-Dimension Associative Arrays • An alternative method in which to create an array is via PHP's array() function: $pairings = array( zinfandel => "Broiled Veal Chops", merlot => "Baked Ham", sauvignon => "Prime Rib", sauternes => "Roasted Salmon"; ); • This assignment method bears no difference in functionality from the previous, other than the format in which it was created. 23
  • 24. Multidimensional Indexed Arrays • Multidimensional indexed arrays function much like their single-dimension counterparts, except that more than one index array is used to specify an element. • There is no limit as to the dimension size. The general syntax of a multidimensional array is: $name[index1] [index2]..[indexN]; • An element of a two-dimensional indexed array could be referenced as follows: $position = $chess_board[5][4]; 24
  • 25. PHP's Operators OPERATOR PURPOSE ++ — Autoincrement, autodecrement / * % Division, multiplication, modulus + − . Addition, subtraction, concatenation < <= Less than, less than or equal to, > >= Greater than, greater than or equal to == != Is equal to, is not equal to === <> identical to, is not equal to && || Boolean AND, boolean OR 25
  • 26. Assignment Operators EXAMPLE OUTCOME $a = 5; $a equals 5 $a += 5; $a equals $a plus 5 $a *= 5; $a equals $a multiplied by 5 $a /= 5; $a equals $a divided by 5 $a .= 5; $a equals $a concatenated with 5 26
  • 27. Conditional statements • Conditional statements are used to perform different actions based on different conditions. ▫ if statement - use this statement to execute some code only if a specified condition is true ▫ if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false ▫ if...elseif....else statement - use this statement to select one of several blocks of code to be executed ▫ switch statement - use this statement to select one of many blocks of code to be executed 27
  • 28. Conditional statements • The if statement - use this statement to execute some code only if a specified condition is true <html> <body> <?php $p=date("D"); if ($p==“Mon”) echo “Today is Monday!"; ?> </body> </html> 28
  • 29. • The if...else Statement • Use the if....else statement to execute some code if a condition is true and another code if a condition is false. if (condition) code to be executed if condition is true; else code to be executed if condition is false; Conditional statements 29
  • 30. • The if...else Statement <html> <body> <?php $p=date(“D”); if ($p==“Mon") { echo “Welcome!<br />”; echo “Today is Monday!”; } else echo “Have a nice day!”; ?> </body> </html> Conditional statements 30
  • 31. • The if...elseif....else Statement • Use the if....elseif...else statement to select one of several blocks of code to be executed. 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; Conditional statements 31
  • 32. <html> <body> <?php $p=date(“D”); if ($p==“Mon") echo “Today is Monday!”; elseif ($p==“Thu") echo “Today is Thursday!”; else echo “Have a nice day!”; ?> </body> </html> Conditional statements 32
  • 33. • The PHP Switch Statement • Use the switch statement to select one of many blocks of code to be executed. switch (n) { case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2; } Conditional statements 33
  • 34. <html> <body> <?php switch ($y) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; default: echo "No number between 1 and 2"; } ?> </body> </html> Conditional statements 34