SlideShare a Scribd company logo
PHP (HYPERTEXT PREPROCESSOR)
Why PHP Uses in server-side scripting. It is platform independent  Advantages of using PHP are: Easy to learn, use, and implement Free for access  Executes on any platform  Executes scripts from the command line and develops client-side GUI applications that can operate on any platform Uses of PHP are application control, database access, file access, and graphics
Start Me Up   These embedded PHP commands are enclosed within special start and end tags.  <?php  ... PHP code ...  ?>
PHP and HTML can be combined  <html>  <head></head>  <body>  Agent: So who do you think you are, anyhow?  <br />  <?php  // print output  echo  'Neo: I am Neo, but my people call me The One.' ;  ?>  </body>  </html>
Output
Comment in PHP code  PHP supports both single-line and multi-line comment blocks:  Example: <?php  // this is a single-line comment  /* and this is a  multi-line  comment */  ?>
A Case of Identity  A variable used to store both numeric and non-numeric data, it can be altered, and can be compared with each other. PHP supports a number of different variable types: integers, floating point numbers, strings and arrays.  Every variable has a name. In PHP, a variable name is preceded by a dollar ($)  Example valid  variable $popeye, $one and $INCOME  invalid  $123 and $48hrs Variable names in PHP are case sensitive, $me is different from $Me or $ME.
example using PHP's variables  <html>  <head></head>  <body>  Agent: So who do you think you are, anyhow?  <br />  <?php  // define variables  $name  =  'Neo' ;  $rank  =  'Anomaly' ;  $serialNumber  =  1 ;  // print output  echo  &quot;Neo: I am <b>$name</b>, the <b>$rank</b>. You can call me by my serial number, <b>$serialNumber</b>.&quot; ;  ?>  </body>  </html>
Output
To assign a value to a variable, We use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables, as below:  <?php  $age = $dob + 15;  ?>
We can also perform more than one assignment at a time. Consider the following example, which assigns three variables the same value simultaneously:  <?php  $angle1 = $angle2 = $angle3 = 60;  ?>
Every language has different types of variable. The language supports a wide variety of data types, including simple numeric, character, string and Boolean types, and more complex arrays and objects. 1.Boolean :  The simplest variable type in PHP, a Boolean variable, simply specifies a true or false value.  <?php  $auth = true;  ?>  2. Integer :  Store number without decimal like 75, -95, 2000 or 1.   <?php  $age = 99;  ?>  Variable-data types
3.Floating-point :  A floating-point number is typically a fractional number such as 12.5 or 3.141592653589. Floating point numbers may be specified using either decimal or scientific notation.  <?php  $temperature = 56.89;  ?>
4. String :  A string is a sequence of characters, like &quot;hello&quot; or “world&quot;.   String values may be enclosed in either double quotes (&quot;&quot;) or single quotes('').  (Quotation marks within the string itself can be &quot;escaped&quot; with a backslash (\) character.)   String values enclosed in double quotes are automatically parsed for special characters and variable names; if these are found, they are replaced with the appropriate value.
example: <?php  $identity  =  'James Bond' ;  $car  =  'BMW' ;  // this would contain the string &quot;James Bond drives a BMW&quot;  $sentence  =  &quot;$identity drives a $car&quot; ;  echo  $sentence ;  ?>   //output James Bond drives a BMW
Market Value  PHP also comes with operators for arithmetic, string, comparison and logical operations. A good way to get familiar with operators is to use them to perform arithmetic operations on variables Below is the example.
<html>  <head>  </head>  <body>  <?php  // set quantity  $quantity  =  1000 ;  // set original and current unit price  $origPrice  =  100 ;  $currPrice  =  25 ;  // calculate difference in price  $diffPrice  =  $currPrice  -  $origPrice ;  // calculate percentage change in price  $diffPricePercent  = (( $currPrice  -  $origPrice ) *  100 )/ $origPrice  ?>
<table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot;>  <tr>  <td>Quantity</td>  <td>Cost price</td>  <td>Current price</td>  <td>Absolute change in price</td>  <td>Percent change in price</td>  </tr>  <tr>  <td> <?php  echo  $quantity ?> </td>  <td> <?php  echo  $origPrice ?> </td>  <td> <?php  echo  $currPrice ?> </td>  <td> <?php  echo  $diffPrice ?> </td>  <td> <?php  echo  $diffPricePercent ?> %</td>  </tr>  </table>  </body>  </html>
Output -75% -75 25 100 1000 Percent change in price Absolute change in price  Current price  Cost price  Quantity
Here is calculations using PHP's various mathematical operators, and stored the results of those calculations in different variables. The rest of the script is related to the display of the resulting calculations in a neat table.  We can even perform an arithmetic operation by using the two operators together.   mathematical operators
Example <?php  // this...  $a  =  5 ;  $a  =  $a  +  10 ;  // ... is the same as this  $a  =  5 ;  $a  +=  10 ;  ?>
Stringing Things Along PHP also allows us to add strings with the string concatenation operator, represented by a period (.).
Example <?php  // set up some string variables  $a  =  'the' ;  $b  =  'games' ;  $c  =  'begin' ;  $d  =  'now' ;  // combine them using the concatenation operator  // this returns 'the games begin now<br />'  $statement  =  $a . ' ' . $b . ' ' . $c . ' ' . $d . '<br />' ;  print  $statement ;  // and this returns 'begin the games now!'  $command  =  $c . ' ' . $a . ' ' . $b . ' ' . $d . '!' ;  print  $command ;  ?>     //Output the games begin now begin the games now!
example <?php  // define string  $str  =  'the' ;  // add and assign  $str  .=  'n' ;  // str now contains &quot;then&quot;  echo  $str ;  ?>   //Output then
Calling function A function is a code that executes when you call it from your program without leaving current program. Example: Calling printf fuction <?php $name =  'Musavir' ; printf( &quot;welcome Mr. %s&quot; ,$name); ?>
FORM A form provides a medium of interface for the client and the server to interact with each other. A form allow the user to actually input raw data into the application. A form consists of the following attributes: Action- specifies the Uniform Resource Locator(URL) that will process the form data and send the feedback.  Method- specifies the way the information is to be sent to the URL User (web browser) Web Server Enter data and the data are sent to Php script engine Passes data Process the data Give response or output
There are two common methods for passing data from one script to another: GET and POST.  The GET method specifies the web browser to send all the user information as part of the URL. In this method, a ? is added at the end of the URL. This ? Indicates the ending of the URL and the beginning of the form information. The POST method specifies the web browser that all the user information is sent through the body of the HTTP request.
This example contains two scripts, one containing an HTML form (named form.htm) and the other containing the form processing logic (message.php).  Here's form.htm:   <html> <head> </head> <body> <form action=&quot;message.php&quot; method=&quot;post&quot;>  Enter your message: <input type=&quot;text&quot; name=&quot;msg&quot; size=&quot;30&quot;>  <input type=&quot;submit&quot; value=&quot;Send&quot;>  </form>  </body> </html>
Here the message.php: <html>  <head></head> <body>  <?php  // retrieve form data  $input  =  $_POST [ 'msg' ];  // use it  echo  &quot;You said: <i>$input</i>&quot; ;  ?>  </body>  </html>
operator An  operator  is a symbol that specifies a particular action in an expression.
 
Comparison operator <?php  /* define some variables */ $mean  =  9 ;  $median  =  10 ;  $mode  =  9 ;  // less-than operator  // returns true if left side is less than right  // returns true here  $result  = ( $mean  <  $median );  print  &quot;result is $result<br />&quot; ;
// greater-than operator  // returns true if left side is greater than right  // returns false here  $result  = ( $mean  >  $median );  print  &quot;result is $result<br />&quot; ;  // less-than-or-equal-to operator  // returns true if left side is less than or equal to right  // returns false here  $result  = ( $median  <=  $mode );  print  &quot;result is $result<br />&quot; ;
// greater-than-or-equal-to operator  // returns true if left side is greater than or equal to right  // returns true here  $result  = ( $median  >=  $mode );  print  &quot;result is $result<br />&quot; ;  // equality operator  // returns true if left side is equal to right  // returns true here  $result  = ( $mean  ==  $mode );  print  &quot;result is $result<br />&quot; ;
The result of a comparison test is always Boolean: either true (1) or false   (0 does not print anything) // not-equal-to operator  // returns true if left side is not equal to right  // returns false here  $result  = ( $mean  !=  $mode );  print  &quot;result is $result<br />&quot; ;  // inequality operator  // returns true if left side is not equal to right  // returns false here  $result  = ( $mean  <>  $mode );  print  &quot;result is $result&quot; ;  ?>
=== operator <?php  /* define two variables */ $str  =  '10' ;  $int  =  10 ;  /* returns true, since both variables contain the same value */  $result  = ( $str  ==  $int );  print  &quot;result is $result<br />&quot; ;  /* returns false, since the variables are not of the same type even though they have the same value */  $result  = ( $str  ===  $int );  print  &quot;result is $result<br />&quot; ;   /* returns true, since the variables are the same type and value */  $anotherInt  =  10 ;  $result  = ( $anotherInt  ===  $int );  print  &quot;result is $result&quot; ;  ?>
Logical operator Four logical operator: AND, OR, XOR, NOT <?php  /* define some variables */ $auth  =  1 ;  $status  =  1 ;  $role  =  4 ;  /* logical AND returns true if all conditions are true */  // returns true  $result  = (( $auth  ==  1 ) && ( $status  !=  0 ));  print  &quot;result is $result<br />&quot; ;
/* logical OR returns true if any condition is true */  // returns true  $result  = (( $status  ==  1 ) || ( $role  <=  2 ));  print  &quot;result is $result<br />&quot; ;  /* logical NOT returns true if the condition is false and vice-versa */  // returns false  $result  = !( $status  ==  1 );  print  &quot;result is $result<br />&quot; ;  /* logical XOR returns true if either of two conditions are true, or returns false if both conditions are true */  // returns false  $result  = (( $status  ==  1 ) xor ( $auth  ==  1 ));  print  &quot;result is $result<br />&quot; ;  ?>
Conditional statement a conditional statement allows you to test whether a specific condition is true or false, and perform different actions on the basis of the result.  if The if conditional is one of the most commonplace constructs of any mainstream programming language, offering a convenient means for conditional code execution. The syntax is: if ( expression ) { statement }
Example: <html>  <head></head> <body>  <form action=&quot;ageist.php&quot; method=&quot;post&quot;>  Enter your age: <input name=&quot;age&quot; size=&quot;2&quot;>  </form>  </body>  </html>
ageist.php <html>  <head></head> <body>  <?php  // retrieve form data  $age  =  $_POST [ 'age' ];  // check entered value and branch  if ( $age  >=  21 ) {       echo  'Come on in, we have alcohol and music awaiting you!' ;  }  if ( $age  <  21 ) {       echo  &quot;You're too young for this club, come back when you're a little older&quot; ;  }  ?>  </body>  </html>
if-else construct, used to define a block of code that gets executed when the conditional expression in the if() statement evaluates as false.  The if-else construct looks like this:  if (condition) {      do this!      }  else {      do this!  }
Example: <html>  <head></head> <body>  <?php  // retrieve form data  $age  =  $_POST [ 'age' ];  // check entered value and branch  if ( $age  >=  21 ) {      echo  'Come on in, we have alcohol and music awaiting you!' ;      }  else {      echo  &quot;You're too young for this club, come back when you're a little older&quot; ;  }  ?>  </body>  </html>
Ternary operator represented by a question mark (?). This operator, which lets you make your conditional statements almost unintelligible, provides shortcut syntax for creating a single-statement if-else block.  <?php  if ( $numTries  >  10 ) {        $msg  =  'Blocking your account...' ;      }  else {       $msg  =  'Welcome!' ;  }  ?>
You could also do this, which is equivalent  <?php  $msg  =  $numTries  >  10  ?  'Blocking your account...'  :  'Welcome!' ;  ?>
Nested if or <?php  if ( $day  ==  'Thursday' ) {      if ( $time  ==  '0800' ) {          if ( $country  ==  'UK' ) {               $meal  =  'bacon and eggs' ;          }      }  }  ?>   <?php  if ( $day  ==  'Thursday'  &&  $time  ==  '0800'  &&  $country  ==  'UK' ) {       $meal  =  'bacon and eggs' ;  }  ?>
<html>  <head></head> <body>  <h2>Today's Special</h2>  <p>  <form method=&quot;get&quot; action=&quot;cooking.php&quot;>  <select name=&quot;day&quot;>  <option value=&quot;1&quot;>Monday/Wednesday  <option value=&quot;2&quot;>Tuesday/Thursday  <option value=&quot;3&quot;>Friday/Sunday  <option value=&quot;4&quot;>Saturday  </select>  <input type=&quot;submit&quot; value=&quot;Send&quot;>  </form>  </body>  </html>
<html> <head></head> <body>  <?php  // get form selection  $day  =  $_GET [ 'day' ];  // check value and select appropriate item  if ( $day  ==  1 ) {       $special  =  'Chicken in oyster sauce' ;      }  elseif ( $day  ==  2 ) {       $special  =  'French onion soup' ;      }  elseif ( $day  ==  3 ) {       $special  =  'Pork chops with mashed potatoes and green salad' ;      }  else {       $special  =  'Fish and chips' ;  }  ?>  <h2>Today's special is:</h2>  <?php  echo  $special ;  ?>  </body> </html>
While() loop While (condition is true) { do this! }
Execution of php statements within curly braces As long as the specified condition is true Condition becomes false The loop will be broken and the following statement will be executed.
Examples <?php $number = 5; while   ($number  >= 2 ) { echo  $number.  &quot;<br/>&quot;  ; $number - = 1; } ?>
Examples Variable is initialized to 5 The while loop executes as long as the condition, ($number>=2) is true At the end of the loop block, the value of $number is decreased by 1.
Output of example
The while() loop executes a set of statements while a specified condition is true. But what happens if the condition is true on the first iteration of the loop itself? If you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the do-while() loop.
Do-while() statement do {     do this! } while (condition is true)
While() vs do-while() Let's take a quick example to better understand the difference between while() and do-while():
Examples <?php $x  =  100 ; // while loop while ( $x  ==  700 ) {     echo  &quot;Running...&quot; ;     break; } ?>
In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $x is not equal to 700. But, if you ran this version of the script:
Example <?php $x  =  100 ; // do-while loop do {     echo  &quot;Running...&quot; ;     break; } while ( $x  ==  700 ); ?>
you would see  one  line of output, as the code within the do() block would run once.
<html> <head></head> <body> <?php // set variables from form input $upperLimit  =  $_POST [ 'limit' ]; $lowerLimit  =  1 ; // keep printing squares until lower limit = upper limit do {     echo ( $lowerLimit  *  $lowerLimit ). '&nbsp;' ;      $lowerLimit ++; } while ( $lowerLimit  <=  $upperLimit ); // print end marker echo  ' END' ; ?> </body> </html>
Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and the condition to be tested is checked afterwards. This implies that the statements within the curly braces would be executed at least once.
For() loops Both the while() and do-while() loops continue to iterate for as long as the specified conditional expression remains true. But what if you need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <td> cells five times? In such cases, clever programmers reach for the for() loop...
The for() loop typically looks like this:  for (initial value of counter; condition; new value of counter) {     do this! }
Examples <html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // define the number $number  =  13 ; // use a for loop to calculate tables for that number for ( $x  =  1 ;  $x  <=  10 ;  $x ++) {     echo  &quot;$number x $x = &quot; .( $number  *  $x ). &quot;<br />&quot; ; } ?> </body> </html>
define the number to be used for the multiplication table.  constructed a for() loop with $x as the counter variable, initialized it to 1.  specified that the loop should run no more than 10 times.  The auto-increment operator  automatically increments the counter by 1 every time the loop is executed. Within the loop, the counter is multiplied by the number,  to create the multiplication table, and echo() is used to display the result on the page.
Switch-case() statement Switch (decision-variable) { case first condition is true: do this! case second condition is true: do this! . . . …  and so on … }
Appropriate case() block execution Depend on the value of decision variable Default block Decision variable not match any of the listed case() conditions
<html> <head></head> <body> <?php // get form selection $day  =  $_GET [ 'day' ]; // check value and select appropriate item switch ( $day ) {     case  1 :          $special  =  'Chicken in oyster sauce' ;         break;     case  2 :          $special  =  'French onion soup' ;         break;     case  3 :          $special  =  'Pork chops with mashed potatoes and green salad' ;         break;     default:          $special  =  'Fish and chips' ;         break; } ?> <h2>Today's special is:</h2> <?php  echo  $special ?> </body> </html>
Summary The  do...while  statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true. The  do...while  loop is similar in nature to the  While  loop. The key difference is that the  do...while  loop body is guaranteed to run at least once. This is possible because the condition statement is evaluated at the end of the loop statement after the loop body is performed.
Summary The  for  statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the  for  loop is known as a definite loop.
Summary In programming it is often necessary to repeat the same block of code a number of times. This can be accomplished using looping statements. PHP includes several types of looping statements.  The  while  statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.
Summary It is often desirable when writing code to perform different actions based on different decision. In addition to the if Statements, PHP includes a fourth type of conditional statement called the  switch  statement. The switch Statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used.

More Related Content

PPT
Eye catching HTML BASICS tips: Learn easily
PPTX
Overview of c++ language
PPTX
Lecture 2 introduction to html
PPTX
Php operators
PDF
HTML practical guide for O/L exam
PDF
Quick flask an intro to flask
PDF
Bca sem 6 php practicals 1to12
PPTX
Eye catching HTML BASICS tips: Learn easily
Overview of c++ language
Lecture 2 introduction to html
Php operators
HTML practical guide for O/L exam
Quick flask an intro to flask
Bca sem 6 php practicals 1to12

What's hot (20)

PPTX
Html ppt
PPT
Learning HTML
PDF
Basic Html Notes
PPT
Introduction to html
PPT
Control Structures In Php 2
PDF
Basic introduction to html and php tags
PPTX
Html ppt
PPTX
Event In JavaScript
ODP
PPT
Html project
PPTX
Html table
DOCX
HTML Web design english & sinhala mix note
PPTX
Difference between frontend and backend
PPT
Php Lecture Notes
PPT
Css Ppt
PPTX
HTML (Web) basics for a beginner
PPT
Span and Div tags in HTML
Html ppt
Learning HTML
Basic Html Notes
Introduction to html
Control Structures In Php 2
Basic introduction to html and php tags
Html ppt
Event In JavaScript
Html project
Html table
HTML Web design english & sinhala mix note
Difference between frontend and backend
Php Lecture Notes
Css Ppt
HTML (Web) basics for a beginner
Span and Div tags in HTML
Ad

Viewers also liked (14)

PDF
Droits Francais
PPTX
App proposal
DOCX
App proposal nathan towel
PDF
Yahoo! App Mobile App Design Teardown
PDF
Everlane iOS App Design Critique
DOC
Khanz developers-golf-mobile-app-proposal
PDF
Time it (App Proposal)
PPTX
App Proposal: Baby Allergy Journal
PDF
Mobile App Project Proposal: Betsy Scherertz
PDF
Sample Guide for Writing Website Development Proposal
DOC
Cd mobile-app-proposal
PPTX
Website design and development company
PDF
Introduction to PHP
PPT
Php Presentation
Droits Francais
App proposal
App proposal nathan towel
Yahoo! App Mobile App Design Teardown
Everlane iOS App Design Critique
Khanz developers-golf-mobile-app-proposal
Time it (App Proposal)
App Proposal: Baby Allergy Journal
Mobile App Project Proposal: Betsy Scherertz
Sample Guide for Writing Website Development Proposal
Cd mobile-app-proposal
Website design and development company
Introduction to PHP
Php Presentation
Ad

Similar to Php Crash Course (20)

PPT
course slides -- powerpoint
PPT
P H P Part I, By Kian
PPT
Open Source Package Php Mysql 1228203701094763 9
PPT
Open Source Package PHP & MySQL
PPT
Basic PHP
PPT
What Is Php
 
PPT
PPT
PPT
PHP Workshop Notes
PPT
Php Training
ODP
Php Learning show
PPT
Introduction to PHP
PPT
Dynamic Web Pages Ch 1 V1.0
PPTX
PHP Powerpoint -- Teach PHP with this
PPT
Web development
PPT
Introduction To Php For Wit2009
PPT
PPT
PHP Tutorials
PPT
PHP Tutorials
course slides -- powerpoint
P H P Part I, By Kian
Open Source Package Php Mysql 1228203701094763 9
Open Source Package PHP & MySQL
Basic PHP
What Is Php
 
PHP Workshop Notes
Php Training
Php Learning show
Introduction to PHP
Dynamic Web Pages Ch 1 V1.0
PHP Powerpoint -- Teach PHP with this
Web development
Introduction To Php For Wit2009
PHP Tutorials
PHP Tutorials

More from mussawir20 (20)

PPT
Php Operators N Controllers
PPT
Php Calling Operators
PPT
Database Design Process
PPT
Php Simple Xml
PPT
Php String And Regular Expressions
PPT
Php Sq Lite
PPT
Php Sessoins N Cookies
PPT
Php Rss
PPT
Php Reusing Code And Writing Functions
PPT
Php Oop
PPT
Php My Sql
PPT
Php File Operations
PPT
Php Error Handling
PPT
Php Basic Security
PPT
Php Using Arrays
PPT
Javascript Oop
PPT
PPT
Javascript
PPT
Object Range
PPT
Prototype Utility Methods(1)
Php Operators N Controllers
Php Calling Operators
Database Design Process
Php Simple Xml
Php String And Regular Expressions
Php Sq Lite
Php Sessoins N Cookies
Php Rss
Php Reusing Code And Writing Functions
Php Oop
Php My Sql
Php File Operations
Php Error Handling
Php Basic Security
Php Using Arrays
Javascript Oop
Javascript
Object Range
Prototype Utility Methods(1)

Recently uploaded (20)

PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Google’s NotebookLM Unveils Video Overviews
PPTX
How to Build Crypto Derivative Exchanges from Scratch.pptx
PDF
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
PDF
Enable Enterprise-Ready Security on IBM i Systems.pdf
PDF
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
PDF
DevOps & Developer Experience Summer BBQ
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
PDF
Event Presentation Google Cloud Next Extended 2025
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Chapter 2 Digital Image Fundamentals.pdf
PPTX
Belt and Road Supply Chain Finance Blockchain Solution
PDF
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
PPTX
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx
Chapter 3 Spatial Domain Image Processing.pdf
CIFDAQ's Teaching Thursday: Moving Averages Made Simple
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
GamePlan Trading System Review: Professional Trader's Honest Take
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Google’s NotebookLM Unveils Video Overviews
How to Build Crypto Derivative Exchanges from Scratch.pptx
How AI Agents Improve Data Accuracy and Consistency in Due Diligence.pdf
Enable Enterprise-Ready Security on IBM i Systems.pdf
ai-archetype-understanding-the-personality-of-agentic-ai.pdf
DevOps & Developer Experience Summer BBQ
NewMind AI Weekly Chronicles - August'25 Week I
ABU RAUP TUGAS TIK kelas 8 hjhgjhgg.pptx
Event Presentation Google Cloud Next Extended 2025
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Chapter 2 Digital Image Fundamentals.pdf
Belt and Road Supply Chain Finance Blockchain Solution
CIFDAQ's Market Wrap: Ethereum Leads, Bitcoin Lags, Institutions Shift
Web Security: Login Bypass, SQLi, CSRF & XSS.pptx

Php Crash Course

  • 2. Why PHP Uses in server-side scripting. It is platform independent Advantages of using PHP are: Easy to learn, use, and implement Free for access Executes on any platform Executes scripts from the command line and develops client-side GUI applications that can operate on any platform Uses of PHP are application control, database access, file access, and graphics
  • 3. Start Me Up These embedded PHP commands are enclosed within special start and end tags. <?php ... PHP code ... ?>
  • 4. PHP and HTML can be combined <html> <head></head> <body> Agent: So who do you think you are, anyhow? <br /> <?php // print output echo 'Neo: I am Neo, but my people call me The One.' ; ?> </body> </html>
  • 6. Comment in PHP code PHP supports both single-line and multi-line comment blocks: Example: <?php // this is a single-line comment /* and this is a multi-line comment */ ?>
  • 7. A Case of Identity A variable used to store both numeric and non-numeric data, it can be altered, and can be compared with each other. PHP supports a number of different variable types: integers, floating point numbers, strings and arrays. Every variable has a name. In PHP, a variable name is preceded by a dollar ($) Example valid variable $popeye, $one and $INCOME invalid $123 and $48hrs Variable names in PHP are case sensitive, $me is different from $Me or $ME.
  • 8. example using PHP's variables <html> <head></head> <body> Agent: So who do you think you are, anyhow? <br /> <?php // define variables $name = 'Neo' ; $rank = 'Anomaly' ; $serialNumber = 1 ; // print output echo &quot;Neo: I am <b>$name</b>, the <b>$rank</b>. You can call me by my serial number, <b>$serialNumber</b>.&quot; ; ?> </body> </html>
  • 10. To assign a value to a variable, We use the assignment operator: the = symbol. This is used to assign a value (the right side of the equation) to a variable (the left side). The value being assigned need not always be fixed; it could also be another variable, an expression, or even an expression involving other variables, as below: <?php $age = $dob + 15; ?>
  • 11. We can also perform more than one assignment at a time. Consider the following example, which assigns three variables the same value simultaneously: <?php $angle1 = $angle2 = $angle3 = 60; ?>
  • 12. Every language has different types of variable. The language supports a wide variety of data types, including simple numeric, character, string and Boolean types, and more complex arrays and objects. 1.Boolean : The simplest variable type in PHP, a Boolean variable, simply specifies a true or false value. <?php $auth = true; ?> 2. Integer : Store number without decimal like 75, -95, 2000 or 1. <?php $age = 99; ?> Variable-data types
  • 13. 3.Floating-point : A floating-point number is typically a fractional number such as 12.5 or 3.141592653589. Floating point numbers may be specified using either decimal or scientific notation. <?php $temperature = 56.89; ?>
  • 14. 4. String : A string is a sequence of characters, like &quot;hello&quot; or “world&quot;. String values may be enclosed in either double quotes (&quot;&quot;) or single quotes(''). (Quotation marks within the string itself can be &quot;escaped&quot; with a backslash (\) character.) String values enclosed in double quotes are automatically parsed for special characters and variable names; if these are found, they are replaced with the appropriate value.
  • 15. example: <?php $identity = 'James Bond' ; $car = 'BMW' ; // this would contain the string &quot;James Bond drives a BMW&quot; $sentence = &quot;$identity drives a $car&quot; ; echo $sentence ; ?> //output James Bond drives a BMW
  • 16. Market Value PHP also comes with operators for arithmetic, string, comparison and logical operations. A good way to get familiar with operators is to use them to perform arithmetic operations on variables Below is the example.
  • 17. <html> <head> </head> <body> <?php // set quantity $quantity = 1000 ; // set original and current unit price $origPrice = 100 ; $currPrice = 25 ; // calculate difference in price $diffPrice = $currPrice - $origPrice ; // calculate percentage change in price $diffPricePercent = (( $currPrice - $origPrice ) * 100 )/ $origPrice ?>
  • 18. <table border=&quot;1&quot; cellpadding=&quot;5&quot; cellspacing=&quot;0&quot;> <tr> <td>Quantity</td> <td>Cost price</td> <td>Current price</td> <td>Absolute change in price</td> <td>Percent change in price</td> </tr> <tr> <td> <?php echo $quantity ?> </td> <td> <?php echo $origPrice ?> </td> <td> <?php echo $currPrice ?> </td> <td> <?php echo $diffPrice ?> </td> <td> <?php echo $diffPricePercent ?> %</td> </tr> </table> </body> </html>
  • 19. Output -75% -75 25 100 1000 Percent change in price Absolute change in price Current price Cost price Quantity
  • 20. Here is calculations using PHP's various mathematical operators, and stored the results of those calculations in different variables. The rest of the script is related to the display of the resulting calculations in a neat table. We can even perform an arithmetic operation by using the two operators together. mathematical operators
  • 21. Example <?php // this... $a = 5 ; $a = $a + 10 ; // ... is the same as this $a = 5 ; $a += 10 ; ?>
  • 22. Stringing Things Along PHP also allows us to add strings with the string concatenation operator, represented by a period (.).
  • 23. Example <?php // set up some string variables $a = 'the' ; $b = 'games' ; $c = 'begin' ; $d = 'now' ; // combine them using the concatenation operator // this returns 'the games begin now<br />' $statement = $a . ' ' . $b . ' ' . $c . ' ' . $d . '<br />' ; print $statement ; // and this returns 'begin the games now!' $command = $c . ' ' . $a . ' ' . $b . ' ' . $d . '!' ; print $command ; ?> //Output the games begin now begin the games now!
  • 24. example <?php // define string $str = 'the' ; // add and assign $str .= 'n' ; // str now contains &quot;then&quot; echo $str ; ?> //Output then
  • 25. Calling function A function is a code that executes when you call it from your program without leaving current program. Example: Calling printf fuction <?php $name = 'Musavir' ; printf( &quot;welcome Mr. %s&quot; ,$name); ?>
  • 26. FORM A form provides a medium of interface for the client and the server to interact with each other. A form allow the user to actually input raw data into the application. A form consists of the following attributes: Action- specifies the Uniform Resource Locator(URL) that will process the form data and send the feedback. Method- specifies the way the information is to be sent to the URL User (web browser) Web Server Enter data and the data are sent to Php script engine Passes data Process the data Give response or output
  • 27. There are two common methods for passing data from one script to another: GET and POST. The GET method specifies the web browser to send all the user information as part of the URL. In this method, a ? is added at the end of the URL. This ? Indicates the ending of the URL and the beginning of the form information. The POST method specifies the web browser that all the user information is sent through the body of the HTTP request.
  • 28. This example contains two scripts, one containing an HTML form (named form.htm) and the other containing the form processing logic (message.php). Here's form.htm: <html> <head> </head> <body> <form action=&quot;message.php&quot; method=&quot;post&quot;> Enter your message: <input type=&quot;text&quot; name=&quot;msg&quot; size=&quot;30&quot;> <input type=&quot;submit&quot; value=&quot;Send&quot;> </form> </body> </html>
  • 29. Here the message.php: <html> <head></head> <body> <?php // retrieve form data $input = $_POST [ 'msg' ]; // use it echo &quot;You said: <i>$input</i>&quot; ; ?> </body> </html>
  • 30. operator An operator is a symbol that specifies a particular action in an expression.
  • 31.  
  • 32. Comparison operator <?php /* define some variables */ $mean = 9 ; $median = 10 ; $mode = 9 ; // less-than operator // returns true if left side is less than right // returns true here $result = ( $mean < $median ); print &quot;result is $result<br />&quot; ;
  • 33. // greater-than operator // returns true if left side is greater than right // returns false here $result = ( $mean > $median ); print &quot;result is $result<br />&quot; ; // less-than-or-equal-to operator // returns true if left side is less than or equal to right // returns false here $result = ( $median <= $mode ); print &quot;result is $result<br />&quot; ;
  • 34. // greater-than-or-equal-to operator // returns true if left side is greater than or equal to right // returns true here $result = ( $median >= $mode ); print &quot;result is $result<br />&quot; ; // equality operator // returns true if left side is equal to right // returns true here $result = ( $mean == $mode ); print &quot;result is $result<br />&quot; ;
  • 35. The result of a comparison test is always Boolean: either true (1) or false (0 does not print anything) // not-equal-to operator // returns true if left side is not equal to right // returns false here $result = ( $mean != $mode ); print &quot;result is $result<br />&quot; ; // inequality operator // returns true if left side is not equal to right // returns false here $result = ( $mean <> $mode ); print &quot;result is $result&quot; ; ?>
  • 36. === operator <?php /* define two variables */ $str = '10' ; $int = 10 ; /* returns true, since both variables contain the same value */ $result = ( $str == $int ); print &quot;result is $result<br />&quot; ; /* returns false, since the variables are not of the same type even though they have the same value */ $result = ( $str === $int ); print &quot;result is $result<br />&quot; ; /* returns true, since the variables are the same type and value */ $anotherInt = 10 ; $result = ( $anotherInt === $int ); print &quot;result is $result&quot; ; ?>
  • 37. Logical operator Four logical operator: AND, OR, XOR, NOT <?php /* define some variables */ $auth = 1 ; $status = 1 ; $role = 4 ; /* logical AND returns true if all conditions are true */ // returns true $result = (( $auth == 1 ) && ( $status != 0 )); print &quot;result is $result<br />&quot; ;
  • 38. /* logical OR returns true if any condition is true */ // returns true $result = (( $status == 1 ) || ( $role <= 2 )); print &quot;result is $result<br />&quot; ; /* logical NOT returns true if the condition is false and vice-versa */ // returns false $result = !( $status == 1 ); print &quot;result is $result<br />&quot; ; /* logical XOR returns true if either of two conditions are true, or returns false if both conditions are true */ // returns false $result = (( $status == 1 ) xor ( $auth == 1 )); print &quot;result is $result<br />&quot; ; ?>
  • 39. Conditional statement a conditional statement allows you to test whether a specific condition is true or false, and perform different actions on the basis of the result. if The if conditional is one of the most commonplace constructs of any mainstream programming language, offering a convenient means for conditional code execution. The syntax is: if ( expression ) { statement }
  • 40. Example: <html> <head></head> <body> <form action=&quot;ageist.php&quot; method=&quot;post&quot;> Enter your age: <input name=&quot;age&quot; size=&quot;2&quot;> </form> </body> </html>
  • 41. ageist.php <html> <head></head> <body> <?php // retrieve form data $age = $_POST [ 'age' ]; // check entered value and branch if ( $age >= 21 ) {      echo 'Come on in, we have alcohol and music awaiting you!' ; } if ( $age < 21 ) {      echo &quot;You're too young for this club, come back when you're a little older&quot; ; } ?> </body> </html>
  • 42. if-else construct, used to define a block of code that gets executed when the conditional expression in the if() statement evaluates as false. The if-else construct looks like this: if (condition) {     do this!     } else {     do this! }
  • 43. Example: <html> <head></head> <body> <?php // retrieve form data $age = $_POST [ 'age' ]; // check entered value and branch if ( $age >= 21 ) {     echo 'Come on in, we have alcohol and music awaiting you!' ;     } else {     echo &quot;You're too young for this club, come back when you're a little older&quot; ; } ?> </body> </html>
  • 44. Ternary operator represented by a question mark (?). This operator, which lets you make your conditional statements almost unintelligible, provides shortcut syntax for creating a single-statement if-else block. <?php if ( $numTries > 10 ) {       $msg = 'Blocking your account...' ;     } else {      $msg = 'Welcome!' ; } ?>
  • 45. You could also do this, which is equivalent <?php $msg = $numTries > 10 ? 'Blocking your account...' : 'Welcome!' ; ?>
  • 46. Nested if or <?php if ( $day == 'Thursday' ) {     if ( $time == '0800' ) {         if ( $country == 'UK' ) {              $meal = 'bacon and eggs' ;         }     } } ?> <?php if ( $day == 'Thursday' && $time == '0800' && $country == 'UK' ) {      $meal = 'bacon and eggs' ; } ?>
  • 47. <html> <head></head> <body> <h2>Today's Special</h2> <p> <form method=&quot;get&quot; action=&quot;cooking.php&quot;> <select name=&quot;day&quot;> <option value=&quot;1&quot;>Monday/Wednesday <option value=&quot;2&quot;>Tuesday/Thursday <option value=&quot;3&quot;>Friday/Sunday <option value=&quot;4&quot;>Saturday </select> <input type=&quot;submit&quot; value=&quot;Send&quot;> </form> </body> </html>
  • 48. <html> <head></head> <body> <?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item if ( $day == 1 ) {      $special = 'Chicken in oyster sauce' ;     } elseif ( $day == 2 ) {      $special = 'French onion soup' ;     } elseif ( $day == 3 ) {      $special = 'Pork chops with mashed potatoes and green salad' ;     } else {      $special = 'Fish and chips' ; } ?> <h2>Today's special is:</h2> <?php echo $special ; ?> </body> </html>
  • 49. While() loop While (condition is true) { do this! }
  • 50. Execution of php statements within curly braces As long as the specified condition is true Condition becomes false The loop will be broken and the following statement will be executed.
  • 51. Examples <?php $number = 5; while ($number >= 2 ) { echo $number. &quot;<br/>&quot; ; $number - = 1; } ?>
  • 52. Examples Variable is initialized to 5 The while loop executes as long as the condition, ($number>=2) is true At the end of the loop block, the value of $number is decreased by 1.
  • 54. The while() loop executes a set of statements while a specified condition is true. But what happens if the condition is true on the first iteration of the loop itself? If you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the do-while() loop.
  • 55. Do-while() statement do {     do this! } while (condition is true)
  • 56. While() vs do-while() Let's take a quick example to better understand the difference between while() and do-while():
  • 57. Examples <?php $x = 100 ; // while loop while ( $x == 700 ) {     echo &quot;Running...&quot; ;     break; } ?>
  • 58. In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $x is not equal to 700. But, if you ran this version of the script:
  • 59. Example <?php $x = 100 ; // do-while loop do {     echo &quot;Running...&quot; ;     break; } while ( $x == 700 ); ?>
  • 60. you would see one line of output, as the code within the do() block would run once.
  • 61. <html> <head></head> <body> <?php // set variables from form input $upperLimit = $_POST [ 'limit' ]; $lowerLimit = 1 ; // keep printing squares until lower limit = upper limit do {     echo ( $lowerLimit * $lowerLimit ). '&nbsp;' ;      $lowerLimit ++; } while ( $lowerLimit <= $upperLimit ); // print end marker echo ' END' ; ?> </body> </html>
  • 62. Thus, the construction of the do-while() loop is such that the statements within the loop are executed first, and the condition to be tested is checked afterwards. This implies that the statements within the curly braces would be executed at least once.
  • 63. For() loops Both the while() and do-while() loops continue to iterate for as long as the specified conditional expression remains true. But what if you need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <td> cells five times? In such cases, clever programmers reach for the for() loop...
  • 64. The for() loop typically looks like this: for (initial value of counter; condition; new value of counter) {     do this! }
  • 65. Examples <html> <head> <basefont face=&quot;Arial&quot;> </head> <body> <?php // define the number $number = 13 ; // use a for loop to calculate tables for that number for ( $x = 1 ; $x <= 10 ; $x ++) {     echo &quot;$number x $x = &quot; .( $number * $x ). &quot;<br />&quot; ; } ?> </body> </html>
  • 66. define the number to be used for the multiplication table. constructed a for() loop with $x as the counter variable, initialized it to 1. specified that the loop should run no more than 10 times. The auto-increment operator automatically increments the counter by 1 every time the loop is executed. Within the loop, the counter is multiplied by the number, to create the multiplication table, and echo() is used to display the result on the page.
  • 67. Switch-case() statement Switch (decision-variable) { case first condition is true: do this! case second condition is true: do this! . . . … and so on … }
  • 68. Appropriate case() block execution Depend on the value of decision variable Default block Decision variable not match any of the listed case() conditions
  • 69. <html> <head></head> <body> <?php // get form selection $day = $_GET [ 'day' ]; // check value and select appropriate item switch ( $day ) {     case 1 :          $special = 'Chicken in oyster sauce' ;         break;     case 2 :          $special = 'French onion soup' ;         break;     case 3 :          $special = 'Pork chops with mashed potatoes and green salad' ;         break;     default:          $special = 'Fish and chips' ;         break; } ?> <h2>Today's special is:</h2> <?php echo $special ?> </body> </html>
  • 70. Summary The do...while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true. The do...while loop is similar in nature to the While loop. The key difference is that the do...while loop body is guaranteed to run at least once. This is possible because the condition statement is evaluated at the end of the loop statement after the loop body is performed.
  • 71. Summary The for statement loop is used when you know how many times you want to execute a statement or a list of statements. For this reason, the for loop is known as a definite loop.
  • 72. Summary In programming it is often necessary to repeat the same block of code a number of times. This can be accomplished using looping statements. PHP includes several types of looping statements. The while statement loops through a block of code as long as a specified condition is evaluated as true. In other words, the while statement will execute a block of code if and as long as a condition is true.
  • 73. Summary It is often desirable when writing code to perform different actions based on different decision. In addition to the if Statements, PHP includes a fourth type of conditional statement called the switch statement. The switch Statement is very similar or an alternative to the if...else if...else commands. The switch statement will test a condition. The outcome of that test will decide which case to execute. Switch is normally used when you are finding an exact (equal) result instead of a greater or less than result. When testing a range of values, the if statement should be used.