SlideShare a Scribd company logo
1 of 36
Download to read offline
hasen@microcis.net July 07, 2013Hassen poreya
Trainer, Cresco Solution
Afghanistan Workforce
Development Program
PHP
Operators, Expressions, Control Statement, Loops
Operators
 A symbol that tells PHP to perform a mathematical
or logical operation.
 Arithmetic
 Logical
 Bitwise
 Miscellaneous
Arithmetic Operators
e.g,
27 / 3
15 % 4
23 % 6
Using a string with an Arithmetic Operator
<?php
$foo = "0"; // $foo is string
$foo = $foo + 2; // $foo is now an integer: 2
$foo = $foo + 1.3; // $foo is now a float: 3.3
$foo = 5 + "10 Books"; // $foo is integer: 15
?>
Using a string with an Arithmetic Operator
++ increment
-- decrement
 If an increment operator precedes the variable, the
variable will be incremented prior to evaluation of the
expression; otherwise, the variable isn’t operated on
until after the value of the expression is computed.
<?php
$count=1;
print($count++); //prints 1, but after print,
count is 2
print(++$count); //prints 3
?>
Logical and Relational Operators
 Relational operators compare values and return either TRUE
or FALSE.
 Logical operators perform logical operations on TRUE and
FALSE
The truth table for operators
Bitwise Operator
 A bit is either 0 or 1
 Bitwise operators view numbers from a binary
perspective.
 Operator Operation Performed
& And
| Or
Bitwise Operator
<?php
$x=14;
$y=7;
$z=$x & $y;
echo “The bitwise (AND) of 14 and 7
is: ”.$z;
?>
Other Operators
Concatenation Operator
 Connect two separated values together.
<?php
$result = “Passed”;
$string = “The exam result of yours
is:”.$result;
print($string);
?>
Variable Variables with $ Operator
<?php
//set variables
$var_name = "myValue";
$myValue = 123.456;
print($$var_name . "<br />");
//prints "123.456“
?>
Assignment Operators
Precedence of Operators
<?php
print((3+2*7) . “<br>”);
//first multiplication
print(((3+2)*7) . “<br>”); //first
addition between parenthesis
?>
Expressions
Expressions
 A bit of PHP that can be evaluated to produce a
value.
 PHP is an expression-oriented language
 Everything is an expression
 Expressions are combinations of identifiers and
operators.
Expressions
 Generally executed from left to right
 Some operators are processed before others.
 Use parentheses to force an operation to occur
first
 Two general rules
 Some operators work only on certain data types.
 If the operation is on a mix of an integer and a double,
the integer will be converted to a double.
Expressions
 PHP will even read doubles with an exponent.
<?php
print((1 + "1") . "<BR>");
print((1 + " 2") . "<BR>");
print((1 + "3extra stuff") . "<BR>");
print((1 + "4.5e6") . "<BR>");
print((1 + "a7") . "<BR>");
?>
Evaluation
 PHP will even read doubles with an exponent.
<?php
print((1 + "1") . "<BR>"); //1 + 1 == 2
print((1 + " 2") . "<BR>"); //1 + 2 == 3
print((1 + "3extra stuff") . "<BR>"); //1 + 3 == 4
print((1 + "4.5e6") . "<BR>"); //1 + 4500000 == 4500001
print((1 + "a7") . "<BR>"); //1 + 0 == 1
//“a7” begins with a letter, PHP treats it as zero!
?>
Control Statement
TRUE and FALSE
 Zero (0 and 0.0) and an empty string ("") are
considered to be FALSE.
 Any other numerical value or string is TRUE.
 Some control statements expect a Boolean value.
The if Statement
If(condition/expression){
statements…
}
 You are not forced to put an elseif or an else
statement after.
<?php
if(($day == “Friday") or ($day == “Saturday"))
{
print(“University is closed");
}
?>
If, ifelse, else Statements
<?php
if ($month == "January") {
$num_month = 1;
}
elseif($month == "February")
$num_month = 2;
// March to October omitted here
elseif($month == "November")
$num_month = 11;
else
$num_month = 12;
?>
switch Statements
Switch(expression){
case case-expression
expression;
break;
case case-expression
expression;
break
…
default
expression;
}
 The expression inside the
switch statement is evaluated
and then compared to each
expression following a case
expression.
 A default statement works
exactly like an else
statement; it matches when
no other case matches.
 Break statement is used
escape from the switch
statement.
Syntax Description
switch Statement
<?php
switch($month) {
case "January":
$num_month = 1;
break;
// February to October omitted here
case "November":
$num_month = 11;
break;
default: // It must be December
$num_month = 12;
}
?>
Loops
 Loops allow you to repeat lines of code based on
some condition.
 Example
 Read lines from a file until the end is reached.
 Print some text a certain number of times.
while Loop Statement
While (condition/expression){
statement;
}
 It is useful when you are not sure how many times
you will need to iterate your statements.
while Loop Statement
<?php
$total = 0;
$i = 1;
while ($i <= 10){
$total += $i++;
}
echo "The sum of 1 to 10 is: " .
$total;
?>
do while statement
do{
statement;
}
while(condition/expression)
 Similar to while loop.
 First the statement is executed, and then acts
according to the condition.
do while Statement
<?php
$total = 0;
$i = 1;
do {
$total += $i++;
}
while ($i <= 10);
echo "The sum of 1 to 10 is : "
. $total;
?>
break Statement
 When a break statement is encountered, the
execution jumps to the outside of the loop or
switch statement.
<?php
while (true){
echo “This line is printed here!”;
break;
echo “This line would never be printed”;
}
//These codes will be executed!
?>
break Statement
<?php
$i=0; $j=0;
while ($i<10){
while($j<5){
if($j==5)
break; // the loop is stopped
$j++; // once $j reaches 5
}
$i++;
}
echo „$i=‟.$i“<br>”;
echo „$j=‟.$i;
?>
continue Statement
 Similar to the break statement
 But only the current execution of the loop is
stopped, after that loop is continued.
<?php
while ($i++<5){
if($i==2)
continue;
echo $i; //2 will not be printed.
}
?>
Let’s codesomething
Class exercise
 Write a while loop that outputs the even numbers
between 1 and 100.
 Rewrite the script for Problem 1 using do while
loop.
 Write a while loop that outputs the numbers from
100 to 0 by 10's.
hasen@microcis.net July 07, 2013Hassen poreya
Trainer, Cresco Solution
Any Questions!

More Related Content

What's hot (20)

Basic PHP
Basic PHPBasic PHP
Basic PHP
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Operators in PHP
Operators in PHPOperators in PHP
Operators in PHP
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Lesson 4 constant
Lesson 4  constantLesson 4  constant
Lesson 4 constant
 
PHP Basic & Variables
PHP Basic & VariablesPHP Basic & Variables
PHP Basic & Variables
 
Php Tutorial
Php TutorialPhp Tutorial
Php Tutorial
 
Lesson 3 php numbers
Lesson 3  php numbersLesson 3  php numbers
Lesson 3 php numbers
 
Lesson 1 php syntax
Lesson 1   php syntaxLesson 1   php syntax
Lesson 1 php syntax
 
Php Lecture Notes
Php Lecture NotesPhp Lecture Notes
Php Lecture Notes
 
PHP
PHPPHP
PHP
 
Php
PhpPhp
Php
 
Functions in PHP
Functions in PHPFunctions in PHP
Functions in PHP
 
PHP Web Programming
PHP Web ProgrammingPHP Web Programming
PHP Web Programming
 
Data types in php
Data types in phpData types in php
Data types in php
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
Operators php
Operators phpOperators php
Operators php
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
Php Calling Operators
Php Calling OperatorsPhp Calling Operators
Php Calling Operators
 

Viewers also liked

Web app development_database_design_10
Web app development_database_design_10Web app development_database_design_10
Web app development_database_design_10Hassen Poreya
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07Hassen Poreya
 
Web app development_my_sql_08
Web app development_my_sql_08Web app development_my_sql_08
Web app development_my_sql_08Hassen Poreya
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03Hassen Poreya
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13Hassen Poreya
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06Hassen Poreya
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04Hassen Poreya
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09Hassen Poreya
 
Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Hassen Poreya
 
Web app development_database_design_11
Web app development_database_design_11Web app development_database_design_11
Web app development_database_design_11Hassen Poreya
 
Learn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresLearn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresTessa Mero
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02Hassen Poreya
 
Web app development_html_01
Web app development_html_01Web app development_html_01
Web app development_html_01Hassen Poreya
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Hassen Poreya
 

Viewers also liked (15)

Web app development_database_design_10
Web app development_database_design_10Web app development_database_design_10
Web app development_database_design_10
 
Web app development_php_07
Web app development_php_07Web app development_php_07
Web app development_php_07
 
Web app development_my_sql_08
Web app development_my_sql_08Web app development_my_sql_08
Web app development_my_sql_08
 
Web app development_html_css_03
Web app development_html_css_03Web app development_html_css_03
Web app development_html_css_03
 
CodeIgniter Practice
CodeIgniter PracticeCodeIgniter Practice
CodeIgniter Practice
 
Web app development_crud_13
Web app development_crud_13Web app development_crud_13
Web app development_crud_13
 
Web app development_php_06
Web app development_php_06Web app development_php_06
Web app development_php_06
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
Web app development_my_sql_09
Web app development_my_sql_09Web app development_my_sql_09
Web app development_my_sql_09
 
Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12Web app development_database_design_er-mapping_12
Web app development_database_design_er-mapping_12
 
Web app development_database_design_11
Web app development_database_design_11Web app development_database_design_11
Web app development_database_design_11
 
Learn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own AdventuresLearn to Code with JavaScript - Choose Your Own Adventures
Learn to Code with JavaScript - Choose Your Own Adventures
 
Web app development_html_02
Web app development_html_02Web app development_html_02
Web app development_html_02
 
Web app development_html_01
Web app development_html_01Web app development_html_01
Web app development_html_01
 
Web app development_cookies_sessions_14
Web app development_cookies_sessions_14Web app development_cookies_sessions_14
Web app development_cookies_sessions_14
 

Similar to PHP Operators, Expressions, Control Statements and Loops

What Is Php
What Is PhpWhat Is Php
What Is PhpAVC
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics McSoftsis
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptxJapneet9
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Muhamad Al Imran
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Muhamad Al Imran
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPwahidullah mudaser
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting languageElmer Concepcion Jr.
 
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.pdfpkaviya
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQLArti Parab Academics
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009cwarren
 

Similar to PHP Operators, Expressions, Control Statements and Loops (20)

What Is Php
What Is PhpWhat Is Php
What Is Php
 
Learn PHP Basics
Learn PHP Basics Learn PHP Basics
Learn PHP Basics
 
PHP Basics
PHP BasicsPHP Basics
PHP Basics
 
Expressions and Operators.pptx
Expressions and Operators.pptxExpressions and Operators.pptx
Expressions and Operators.pptx
 
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
Php i basic chapter 3 (syahir chaer's conflicted copy 2013-04-22)
 
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
Php i basic chapter 3 (afifah rosli's conflicted copy 2013-04-23)
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 
Javascript basics
Javascript basicsJavascript basics
Javascript basics
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
PHP - Web Development
PHP - Web DevelopmentPHP - Web Development
PHP - Web Development
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
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
 
Php essentials
Php essentialsPhp essentials
Php essentials
 
Web development
Web developmentWeb development
Web development
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Introduction To Php For Wit2009
Introduction To Php For Wit2009Introduction To Php For Wit2009
Introduction To Php For Wit2009
 
php 1
php 1php 1
php 1
 
Php Learning show
Php Learning showPhp Learning show
Php Learning show
 

Recently uploaded

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 

Recently uploaded (20)

Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 

PHP Operators, Expressions, Control Statements and Loops

  • 1. hasen@microcis.net July 07, 2013Hassen poreya Trainer, Cresco Solution Afghanistan Workforce Development Program PHP Operators, Expressions, Control Statement, Loops
  • 2. Operators  A symbol that tells PHP to perform a mathematical or logical operation.  Arithmetic  Logical  Bitwise  Miscellaneous
  • 4. Using a string with an Arithmetic Operator <?php $foo = "0"; // $foo is string $foo = $foo + 2; // $foo is now an integer: 2 $foo = $foo + 1.3; // $foo is now a float: 3.3 $foo = 5 + "10 Books"; // $foo is integer: 15 ?>
  • 5. Using a string with an Arithmetic Operator ++ increment -- decrement  If an increment operator precedes the variable, the variable will be incremented prior to evaluation of the expression; otherwise, the variable isn’t operated on until after the value of the expression is computed. <?php $count=1; print($count++); //prints 1, but after print, count is 2 print(++$count); //prints 3 ?>
  • 6. Logical and Relational Operators  Relational operators compare values and return either TRUE or FALSE.  Logical operators perform logical operations on TRUE and FALSE
  • 7. The truth table for operators
  • 8. Bitwise Operator  A bit is either 0 or 1  Bitwise operators view numbers from a binary perspective.  Operator Operation Performed & And | Or
  • 9. Bitwise Operator <?php $x=14; $y=7; $z=$x & $y; echo “The bitwise (AND) of 14 and 7 is: ”.$z; ?>
  • 11. Concatenation Operator  Connect two separated values together. <?php $result = “Passed”; $string = “The exam result of yours is:”.$result; print($string); ?>
  • 12. Variable Variables with $ Operator <?php //set variables $var_name = "myValue"; $myValue = 123.456; print($$var_name . "<br />"); //prints "123.456“ ?>
  • 14. Precedence of Operators <?php print((3+2*7) . “<br>”); //first multiplication print(((3+2)*7) . “<br>”); //first addition between parenthesis ?>
  • 16. Expressions  A bit of PHP that can be evaluated to produce a value.  PHP is an expression-oriented language  Everything is an expression  Expressions are combinations of identifiers and operators.
  • 17. Expressions  Generally executed from left to right  Some operators are processed before others.  Use parentheses to force an operation to occur first  Two general rules  Some operators work only on certain data types.  If the operation is on a mix of an integer and a double, the integer will be converted to a double.
  • 18. Expressions  PHP will even read doubles with an exponent. <?php print((1 + "1") . "<BR>"); print((1 + " 2") . "<BR>"); print((1 + "3extra stuff") . "<BR>"); print((1 + "4.5e6") . "<BR>"); print((1 + "a7") . "<BR>"); ?>
  • 19. Evaluation  PHP will even read doubles with an exponent. <?php print((1 + "1") . "<BR>"); //1 + 1 == 2 print((1 + " 2") . "<BR>"); //1 + 2 == 3 print((1 + "3extra stuff") . "<BR>"); //1 + 3 == 4 print((1 + "4.5e6") . "<BR>"); //1 + 4500000 == 4500001 print((1 + "a7") . "<BR>"); //1 + 0 == 1 //“a7” begins with a letter, PHP treats it as zero! ?>
  • 21. TRUE and FALSE  Zero (0 and 0.0) and an empty string ("") are considered to be FALSE.  Any other numerical value or string is TRUE.  Some control statements expect a Boolean value.
  • 22. The if Statement If(condition/expression){ statements… }  You are not forced to put an elseif or an else statement after. <?php if(($day == “Friday") or ($day == “Saturday")) { print(“University is closed"); } ?>
  • 23. If, ifelse, else Statements <?php if ($month == "January") { $num_month = 1; } elseif($month == "February") $num_month = 2; // March to October omitted here elseif($month == "November") $num_month = 11; else $num_month = 12; ?>
  • 24. switch Statements Switch(expression){ case case-expression expression; break; case case-expression expression; break … default expression; }  The expression inside the switch statement is evaluated and then compared to each expression following a case expression.  A default statement works exactly like an else statement; it matches when no other case matches.  Break statement is used escape from the switch statement. Syntax Description
  • 25. switch Statement <?php switch($month) { case "January": $num_month = 1; break; // February to October omitted here case "November": $num_month = 11; break; default: // It must be December $num_month = 12; } ?>
  • 26. Loops  Loops allow you to repeat lines of code based on some condition.  Example  Read lines from a file until the end is reached.  Print some text a certain number of times.
  • 27. while Loop Statement While (condition/expression){ statement; }  It is useful when you are not sure how many times you will need to iterate your statements.
  • 28. while Loop Statement <?php $total = 0; $i = 1; while ($i <= 10){ $total += $i++; } echo "The sum of 1 to 10 is: " . $total; ?>
  • 29. do while statement do{ statement; } while(condition/expression)  Similar to while loop.  First the statement is executed, and then acts according to the condition.
  • 30. do while Statement <?php $total = 0; $i = 1; do { $total += $i++; } while ($i <= 10); echo "The sum of 1 to 10 is : " . $total; ?>
  • 31. break Statement  When a break statement is encountered, the execution jumps to the outside of the loop or switch statement. <?php while (true){ echo “This line is printed here!”; break; echo “This line would never be printed”; } //These codes will be executed! ?>
  • 32. break Statement <?php $i=0; $j=0; while ($i<10){ while($j<5){ if($j==5) break; // the loop is stopped $j++; // once $j reaches 5 } $i++; } echo „$i=‟.$i“<br>”; echo „$j=‟.$i; ?>
  • 33. continue Statement  Similar to the break statement  But only the current execution of the loop is stopped, after that loop is continued. <?php while ($i++<5){ if($i==2) continue; echo $i; //2 will not be printed. } ?>
  • 35. Class exercise  Write a while loop that outputs the even numbers between 1 and 100.  Rewrite the script for Problem 1 using do while loop.  Write a while loop that outputs the numbers from 100 to 0 by 10's.
  • 36. hasen@microcis.net July 07, 2013Hassen poreya Trainer, Cresco Solution Any Questions!