SlideShare a Scribd company logo
Hypertext preprocessor
INTRODUCTION “ PHP is an HTML-embedded scripting language.”  The goal of the language is to allow web developers to write dynamically generated pages quickly."
SYNTAX All PHP code must be contained within the following... <?php ?>  or the shorthand PHP tag that requires  shorthand support to be enabled on your server... <? ?> If you have PHP inserted into your HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension. The semicolon signifies the end of a PHP statement and should never be forgotten.
Example of php code <html> <head> <title>My First PHP Page</title> </head> <body> <?php echo &quot;Hello World!&quot;; ?> </body> </html> Display:  Hello World!  If you save this file (e.g. helloworld.php) and place it on PHP enabled server and load it up in your web browser, then you should see &quot;Hello World!&quot; displayed.
<html> <head> <title>My First PHP Page</title> </head> <body> <?php echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; ?> </body> </html>  Display :  Hello World! Hello World! Hello World! Hello world! Hello world!
White Space Whitespace is ignored between PHP statements. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well.  <?php echo &quot;Hello World!&quot;;  echo &quot;Hello World!&quot;; ?> Display: Hello World!Hello World!
variables A variable is a means of storing a value, such as text string &quot;Hello World!&quot; or the integer value 4.  In PHP you define a variable with the following form: $variable_name = Value; If you forget that dollar sign at the beginning, it will not work.  variable names are case-sensitive, so use the exact same capitalization when using a variable.  The variables $a_number and $A_number are different variables in PHP's eyes.
There are a few rules that you need to follow when choosing a name for your PHP variables. * PHP variables must start with a letter or underscore &quot;_&quot;. * PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ . * Variables with more than one word should be separatedwith underscores. $my_variable * Variables with more than one word can also be distinguished with capitalization. $myVariable
Outputting a String To output a string, we use PHP echo. PHP Code: <?php $myString = &quot;Hello!&quot;; echo $myString; echo &quot;<h5>I love using PHP!</h5>&quot;; ?> Display: Hello! I love using PHP!
Careful When Echoing Quotes! you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes!  Echo uses quotes to define the beginning and end of the string  use one of the following tactics if the string contains quotations: * Don't use quotes inside your string * Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \&quot; * Use single quotes (apostrophes) for quotes inside your string.
PHP Code: <?php // This won't work because of the quotes around specialH5! echo &quot;<h5 class=&quot;specialH5&quot;>I love using PHP!</h5>&quot;;  // OK because we escaped the quotes! echo &quot;<h5 class=\&quot;specialH5\&quot;>I love using PHP!</h5>&quot;;  // OK because we used an apostrophe ' echo &quot;<h5 class='specialH5'>I love using PHP!</h5>&quot;; ?> If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the quotations by placing a backslash in front of it ( \&quot; ).  The backslash will tell PHP that you want the quotation to be used within the string and NOT to be used to end echo's string.
Echoing Variables No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable. PHP Code: <?php $my_string = &quot;Hello Bob.  My name is: &quot;; $my_number = 4; $my_letter = a; echo $my_string; echo $my_number; echo $my_letter; ?> Display:  Hello Bob. My name is: 4a
Echoing Variables and Text Strings By putting a variable inside the quotes (&quot; &quot;) you are telling PHP that you want it to grab the string value of that variable and use it in the string.  PHP Code: <?php $my_string = &quot;Hello Bob.  My name is: &quot;; echo &quot;$my_string Bobettta <br />&quot;; echo &quot;Hi, I'm Bob.  Who are you? $my_string <br/>&quot;; echo &quot;Hi, I'm Bob.  Who are you? $my_string Bobetta&quot;; ?>
Display Hi, I'm Bob. Who are you? Hello Bob. My name is:Bobetta Display: Hello Bob. My name is: Bobetta By placing variables inside a string you can save yourself some time and make your code easier to read, though it does take some getting used to. Remember to use double-quotes, single-quotes will not grab the value of the string. Single-quotes will just output the variable name to the string, like  )$ my_string), rather than (Hello Bob. My name is: ).
PHP - String Creation string can be used directly in a function or it can be stored in a variable.  Below we create the exact same string twice: first storing it into a variable and in the second case we send the string directly to echo. PHP Code: $my_string = &quot;Tizag - Unlock your potential!&quot;; echo &quot;Tizag - Unlock your potential!&quot;; echo $my_string;
In the above example the first string will be stored into the variable $my_string, while the second string will be used in the echo and not be stored.  Below is the output from the example code. Display: Tizag - Unlock your potential! Tizag - Unlock your potential!
Php operators There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all. * Assignment Operators * Arithmetic Operators * Comparison Operators * String Operators * Combination Arithmetic & Assignment Operators
Assignment Operators Assignment operators are used to set a variable equal to a value or set a variable to another variable's value.  Such an assignment of value is done with the &quot;=&quot;, or equal character. Example: * $my_var = 4; * $another_var = $my_var; Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
Arithmetic Operators Operator   Name Example +      Addition  2 + 4 -  Subtraction  6 - 2 *  Multiplication  5 * 3 /  Division  15 / 3 % Modulus  43 % 10
Example PHP Code: $addition = 2 + 4; $subtraction = 6 - 2;  $multiplication = 5 * 3;  $division = 15 / 3;  $modulus = 5 % 2;  echo &quot;Perform addition: 2 + 4 = &quot;.$addition.&quot;<br />&quot;;  echo &quot;Perform subtraction: 6 - 2 = &quot;.$subtraction.&quot;<br />&quot;;  echo &quot;Perform multiplication:  5 * 3 = &quot;.$multiplication.&quot;<br />&quot;;  echo &quot;Perform division: 15 / 3 = &quot;.$division.&quot;<br />&quot;;  echo &quot;Perform modulus: 5 % 2 = &quot; . $modulus  . &quot;. Modulus is the remainder after the division operation has been performed.  In this case it was 5 / 2, which has a remainder of 1.&quot;;
Display: Perform addition: 2 + 4 = 6 Perform subtraction: 6 - 2 = 4 Perform multiplication: 5 * 3 = 15 Perform division: 15 / 3 = 5 Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.
Comparison Operators Comparisons are used to check the relationship between variables and/or values.  Assume: $x = 4 and $y = 5; Operator Example  Result ==  $x == $y  false !=    $x != $y  true <  $x < $y  true >  $x > $y  false <=    $x <= $y  true >=    $x >= $y    false
String Operators The  period &quot;.&quot; is used to add two strings together, or more technically, the period is the concatenation operator for strings. PHP Code: $a_string = &quot;Hello&quot;; $another_string = &quot; Billy&quot;; $new_string = $a_string . $another_string; echo $new_string . &quot;!&quot;; Display:  Hello Billy!
Pre/Post-Increment & Pre/Post-Decrement To add one to a variable or &quot;increment&quot; use the &quot;++&quot; operator: * $x++; Which is equivalent to $x += 1; or $x = $x + 1; To subtract 1 from a variable, or &quot;decrement&quot; use the &quot;--&quot; operator: * $x--; Which is equivalent to $x -= 1; or $x = $x - 1;
PHP Code: $x = 4; echo &quot;The value of x with post-plusplus = &quot; . $x++; echo &quot;<br /> The value of x after the post-plusplus is &quot; . $x; $x = 4; echo &quot;<br />The value of x with with pre-plusplus = &quot; . ++$x; echo &quot;<br /> The value of x after the pre-plusplus is &quot; . $x; Display: The value of x with post-plusplus = 4 The value of x after the post-plusplus is = 5 The value of x with with pre-plusplus = 5  The value of x after the pre-plusplus is = 5
Php include Say we wanted to create a common menu file that all our pages will use. A common practice for naming files that are to be included is to use the &quot;.php&quot; extension. Since we want to create a common menu let's save it as &quot;menu.php&quot;. menu.php code: <html> <body> <a href=&quot;http://www.example.com/index.php&quot;>Home</a> -  <a href=&quot;http://www.example.com/about.php&quot;>About Us</a> -  <a href=&quot;http://www.example.com/links.php&quot;>Links</a> -  <a href=&quot;http://www.example.com/contact.php&quot;>Contact Us</a> <br />
...continued index.php Code: <?php include(&quot;menu.php&quot;); ?> <p>This is my home page that uses a common menu to save me time when I add new pages to my website!</p> </body> </html> Display: Home - About Us - Links - Contact Us This is my home page that uses a common menu to save me time when I add new pages to my website!
Php forms order.html Code: <html> <body> <h4>Tizag Art Supply Order Form</h4> <form>  <select> <option>Paint</option> <option>Brushes</option> <option>Erasers</option></select> Quantity: <input type=&quot;text&quot; />  <input type=&quot;submit&quot; /> </form> </body> </html>
PHP Form Processor We want to get the &quot;item&quot; and &quot;quantity&quot; inputs that we have specified in our HTML form.  Using an associative array (this term is explained in the array lesson), we can get this information from the $_POST associative array. The proper way to get this information would be to create two new variables, $item and $quantity and set them equal to the values that have been &quot;posted&quot;. The name of this file is &quot;process.php&quot;.
process.php Code: <html><body> <?php $quantity = $_POST['quantity']; $item = $_POST['item']; echo &quot;You ordered &quot;. $quantity . &quot; &quot; . $item . &quot;.<br />&quot;; echo &quot;Thank you for ordering from Tizag Art Supplies!&quot;; ?> </body></html> As you probably noticed, the name in $_POST['name'] corresponds to the name that we specified in our HTML form.
Now try uploading the &quot;order.html&quot; and &quot;process.php&quot; files to a PHP enabled server and test them out.  If someone selected the item brushes and specified a quantity of 6, then the following would be displayed on &quot;process.php&quot;: process.php Code: You ordered 6 brushes. Thank you for ordering from Tizag Art Supplies!
Php function PHP Code with Function: <?php function myCompanyMotto(){ echo &quot;We deliver quantity, not quality!<br />&quot;; } echo &quot;Welcome to Tizag.com <br />&quot;; myCompanyMotto(); echo &quot;Well, thanks for stopping by! <br />&quot;; echo &quot;and remember... <br />&quot;; myCompanyMotto(); ?>
Display: Welcome to Tizag.com We deliver quantity, not quality! Well, thanks for stopping by! and remember... We deliver quantity, not quality! # Always start your function with the keyword function # Remember that your function's code must be between the &quot;{&quot; and the &quot;}&quot;
Php function parameters if we use parameters, then we add some extra functionality! A parameter appears with the parentheses &quot;( )&quot; and looks just like a normal PHP variable. Our parameter will be the person's name and our function will concatenate this name onto a greeting string.  PHP Code with Function: <?php function myGreeting($firstName){ echo &quot;Hello there &quot;. $firstName . &quot;!<br />&quot;; } ?>
PHP Code: <?php function mySum($numX, $numY){ $total = $numX + $numY; return $total; } $myNumber = 0; echo &quot;Before the function, myNumber = &quot;. $myNumber .&quot;<br/>&quot;; $myNumber = mySum(3, 4); // Store the result of mySum in $myNumber echo &quot;After the function, myNumber = &quot; . $myNumber .&quot;<br/>&quot;; ?> Display: Before the function, myNumber = 0 After the function, myNumber = 7
Php files In PHP, a file is created using a command that is also used to open files. It may seem a little confusing, but we'll try to clarify this conundrum. In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call.  So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending.
Creating files The fopen function needs two important pieces of information to operate correctly.  First, we must supply it with the name of the file that we want it to open.  Secondly, we must tell the function what we plan on doing with that file (i.e. read from the file, write information, etc). Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file.  Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.
PHP Code: $ourFileName = &quot;testFile.txt&quot;; $ourFileHandle = fopen($ourFileName, 'w') or die(&quot;can't open file&quot;); fclose($ourFileHandle); The file &quot;testFile.txt&quot; should be created in the same directory where this PHP code resides.  PHP will see that &quot;testFile.txt&quot; does not exist and will create it after running this code.  There's a lot of information in those three lines of code, let's make sure you understand it.
PHP Sessions  When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using PHP sessions. A normal HTML website will not pass data from one page to another.  This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next.
...continued Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID.  This helps to prevent two users' data from getting confused with one another when visiting the same webpage. If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.
Starting a PHP Session Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. Below is a simple script that you should place at the beginning of your PHP code to start up a PHP session. PHP Code: <?php session_start(); // start up your PHP session!  ?> This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session.

More Related Content

PPT
Babitha5.php
PPTX
John Rowley Notes
PPT
Dynamic Web Pages Ch 1 V1.0
PPT
Web development
PPT
Php essentials
PPTX
Php Tutorial
PPTX
PHP tutorial | ptutorial
Babitha5.php
John Rowley Notes
Dynamic Web Pages Ch 1 V1.0
Web development
Php essentials
Php Tutorial
PHP tutorial | ptutorial

What's hot (15)

PPT
P H P Part I, By Kian
PDF
Php tutorial
PPTX
Php introduction and configuration
DOCX
PHP Lesson
PDF
Php tutorial(w3schools)
PPT
Introduction to PHP
PDF
php_tizag_tutorial
PPTX
PHP 5.3
PPT
PPT
Introduction to PHP
PPTX
Php Loop
PPT
Open Source Package Php Mysql 1228203701094763 9
PPTX
Welcome to computer programmer 2
PPT
Php i basic chapter 3
P H P Part I, By Kian
Php tutorial
Php introduction and configuration
PHP Lesson
Php tutorial(w3schools)
Introduction to PHP
php_tizag_tutorial
PHP 5.3
Introduction to PHP
Php Loop
Open Source Package Php Mysql 1228203701094763 9
Welcome to computer programmer 2
Php i basic chapter 3
Ad

Viewers also liked (7)

PPTX
Node.js for .net developers
PPT
PPTX
Cover partition lm
PPT
Php Training
PDF
Sql Training
PPTX
My Saminar On Php
PPTX
இரட்டைக் கிளவியும்
Node.js for .net developers
Cover partition lm
Php Training
Sql Training
My Saminar On Php
இரட்டைக் கிளவியும்
Ad

Similar to Php (20)

PPT
Php Crash Course
PPT
What Is Php
 
PPT
Babitha5.php
PPT
Babitha5.php
PPT
Control Structures In Php 2
PPT
PHP Tutorials
PPT
PHP Tutorials
PPT
Open Source Package PHP & MySQL
PPT
02 Php Vars Op Control Etc
PPT
PPT
Introduction To Lamp
ODP
Php Learning show
PPT
ODP
Php1
PPTX
PHP BASICs Data Type ECHo and PRINT.pptx
PDF
Lecture14-Introduction to PHP-coding.pdf
PDF
Programming in PHP Course Material BCA 6th Semester
Php Crash Course
What Is Php
 
Babitha5.php
Babitha5.php
Control Structures In Php 2
PHP Tutorials
PHP Tutorials
Open Source Package PHP & MySQL
02 Php Vars Op Control Etc
Introduction To Lamp
Php Learning show
Php1
PHP BASICs Data Type ECHo and PRINT.pptx
Lecture14-Introduction to PHP-coding.pdf
Programming in PHP Course Material BCA 6th Semester

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
Lesson notes of climatology university.
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Orientation - ARALprogram of Deped to the Parents.pptx
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Trump Administration's workforce development strategy
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Chinmaya Tiranga quiz Grand Finale.pdf
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Final Presentation General Medicine 03-08-2024.pptx
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Lesson notes of climatology university.
O5-L3 Freight Transport Ops (International) V1.pdf
Orientation - ARALprogram of Deped to the Parents.pptx
A systematic review of self-coping strategies used by university students to ...
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Trump Administration's workforce development strategy
O7-L3 Supply Chain Operations - ICLT Program
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Chinmaya Tiranga quiz Grand Finale.pdf
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
human mycosis Human fungal infections are called human mycosis..pptx
Final Presentation General Medicine 03-08-2024.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Yogi Goddess Pres Conference Studio Updates
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Final Presentation General Medicine 03-08-2024.pptx

Php

  • 2. INTRODUCTION “ PHP is an HTML-embedded scripting language.” The goal of the language is to allow web developers to write dynamically generated pages quickly.&quot;
  • 3. SYNTAX All PHP code must be contained within the following... <?php ?> or the shorthand PHP tag that requires shorthand support to be enabled on your server... <? ?> If you have PHP inserted into your HTML and want the web browser to interpret it correctly, then you must save the file with a .php extension, instead of the standard .html extension. The semicolon signifies the end of a PHP statement and should never be forgotten.
  • 4. Example of php code <html> <head> <title>My First PHP Page</title> </head> <body> <?php echo &quot;Hello World!&quot;; ?> </body> </html> Display: Hello World! If you save this file (e.g. helloworld.php) and place it on PHP enabled server and load it up in your web browser, then you should see &quot;Hello World!&quot; displayed.
  • 5. <html> <head> <title>My First PHP Page</title> </head> <body> <?php echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; echo &quot;Hello World! &quot;; ?> </body> </html> Display : Hello World! Hello World! Hello World! Hello world! Hello world!
  • 6. White Space Whitespace is ignored between PHP statements. You can also press tab to indent your code and the PHP interpreter will ignore those spaces as well. <?php echo &quot;Hello World!&quot;; echo &quot;Hello World!&quot;; ?> Display: Hello World!Hello World!
  • 7. variables A variable is a means of storing a value, such as text string &quot;Hello World!&quot; or the integer value 4. In PHP you define a variable with the following form: $variable_name = Value; If you forget that dollar sign at the beginning, it will not work. variable names are case-sensitive, so use the exact same capitalization when using a variable. The variables $a_number and $A_number are different variables in PHP's eyes.
  • 8. There are a few rules that you need to follow when choosing a name for your PHP variables. * PHP variables must start with a letter or underscore &quot;_&quot;. * PHP variables may only be comprised of alpha-numeric characters and underscores. a-z, A-Z, 0-9, or _ . * Variables with more than one word should be separatedwith underscores. $my_variable * Variables with more than one word can also be distinguished with capitalization. $myVariable
  • 9. Outputting a String To output a string, we use PHP echo. PHP Code: <?php $myString = &quot;Hello!&quot;; echo $myString; echo &quot;<h5>I love using PHP!</h5>&quot;; ?> Display: Hello! I love using PHP!
  • 10. Careful When Echoing Quotes! you can output HTML with PHP. However, you must be careful when using HTML code or any other string that includes quotes! Echo uses quotes to define the beginning and end of the string use one of the following tactics if the string contains quotations: * Don't use quotes inside your string * Escape your quotes that are within the string with a backslash. To escape a quote just place a backslash directly before the quotation mark, i.e. \&quot; * Use single quotes (apostrophes) for quotes inside your string.
  • 11. PHP Code: <?php // This won't work because of the quotes around specialH5! echo &quot;<h5 class=&quot;specialH5&quot;>I love using PHP!</h5>&quot;; // OK because we escaped the quotes! echo &quot;<h5 class=\&quot;specialH5\&quot;>I love using PHP!</h5>&quot;; // OK because we used an apostrophe ' echo &quot;<h5 class='specialH5'>I love using PHP!</h5>&quot;; ?> If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape the quotations by placing a backslash in front of it ( \&quot; ). The backslash will tell PHP that you want the quotation to be used within the string and NOT to be used to end echo's string.
  • 12. Echoing Variables No quotations are required, even if the variable does not hold a string. Below is the correct format for echoing a variable. PHP Code: <?php $my_string = &quot;Hello Bob. My name is: &quot;; $my_number = 4; $my_letter = a; echo $my_string; echo $my_number; echo $my_letter; ?> Display: Hello Bob. My name is: 4a
  • 13. Echoing Variables and Text Strings By putting a variable inside the quotes (&quot; &quot;) you are telling PHP that you want it to grab the string value of that variable and use it in the string. PHP Code: <?php $my_string = &quot;Hello Bob. My name is: &quot;; echo &quot;$my_string Bobettta <br />&quot;; echo &quot;Hi, I'm Bob. Who are you? $my_string <br/>&quot;; echo &quot;Hi, I'm Bob. Who are you? $my_string Bobetta&quot;; ?>
  • 14. Display Hi, I'm Bob. Who are you? Hello Bob. My name is:Bobetta Display: Hello Bob. My name is: Bobetta By placing variables inside a string you can save yourself some time and make your code easier to read, though it does take some getting used to. Remember to use double-quotes, single-quotes will not grab the value of the string. Single-quotes will just output the variable name to the string, like )$ my_string), rather than (Hello Bob. My name is: ).
  • 15. PHP - String Creation string can be used directly in a function or it can be stored in a variable. Below we create the exact same string twice: first storing it into a variable and in the second case we send the string directly to echo. PHP Code: $my_string = &quot;Tizag - Unlock your potential!&quot;; echo &quot;Tizag - Unlock your potential!&quot;; echo $my_string;
  • 16. In the above example the first string will be stored into the variable $my_string, while the second string will be used in the echo and not be stored. Below is the output from the example code. Display: Tizag - Unlock your potential! Tizag - Unlock your potential!
  • 17. Php operators There are many operators used in PHP, so we have separated them into the following categories to make it easier to learn them all. * Assignment Operators * Arithmetic Operators * Comparison Operators * String Operators * Combination Arithmetic & Assignment Operators
  • 18. Assignment Operators Assignment operators are used to set a variable equal to a value or set a variable to another variable's value. Such an assignment of value is done with the &quot;=&quot;, or equal character. Example: * $my_var = 4; * $another_var = $my_var; Now both $my_var and $another_var contain the value 4. Assignments can also be used in conjunction with arithmetic operators.
  • 19. Arithmetic Operators Operator Name Example + Addition 2 + 4 - Subtraction 6 - 2 * Multiplication 5 * 3 / Division 15 / 3 % Modulus 43 % 10
  • 20. Example PHP Code: $addition = 2 + 4; $subtraction = 6 - 2; $multiplication = 5 * 3; $division = 15 / 3; $modulus = 5 % 2; echo &quot;Perform addition: 2 + 4 = &quot;.$addition.&quot;<br />&quot;; echo &quot;Perform subtraction: 6 - 2 = &quot;.$subtraction.&quot;<br />&quot;; echo &quot;Perform multiplication: 5 * 3 = &quot;.$multiplication.&quot;<br />&quot;; echo &quot;Perform division: 15 / 3 = &quot;.$division.&quot;<br />&quot;; echo &quot;Perform modulus: 5 % 2 = &quot; . $modulus . &quot;. Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.&quot;;
  • 21. Display: Perform addition: 2 + 4 = 6 Perform subtraction: 6 - 2 = 4 Perform multiplication: 5 * 3 = 15 Perform division: 15 / 3 = 5 Perform modulus: 5 % 2 = 1. Modulus is the remainder after the division operation has been performed. In this case it was 5 / 2, which has a remainder of 1.
  • 22. Comparison Operators Comparisons are used to check the relationship between variables and/or values. Assume: $x = 4 and $y = 5; Operator Example Result == $x == $y false != $x != $y true < $x < $y true > $x > $y false <= $x <= $y true >= $x >= $y false
  • 23. String Operators The period &quot;.&quot; is used to add two strings together, or more technically, the period is the concatenation operator for strings. PHP Code: $a_string = &quot;Hello&quot;; $another_string = &quot; Billy&quot;; $new_string = $a_string . $another_string; echo $new_string . &quot;!&quot;; Display: Hello Billy!
  • 24. Pre/Post-Increment & Pre/Post-Decrement To add one to a variable or &quot;increment&quot; use the &quot;++&quot; operator: * $x++; Which is equivalent to $x += 1; or $x = $x + 1; To subtract 1 from a variable, or &quot;decrement&quot; use the &quot;--&quot; operator: * $x--; Which is equivalent to $x -= 1; or $x = $x - 1;
  • 25. PHP Code: $x = 4; echo &quot;The value of x with post-plusplus = &quot; . $x++; echo &quot;<br /> The value of x after the post-plusplus is &quot; . $x; $x = 4; echo &quot;<br />The value of x with with pre-plusplus = &quot; . ++$x; echo &quot;<br /> The value of x after the pre-plusplus is &quot; . $x; Display: The value of x with post-plusplus = 4 The value of x after the post-plusplus is = 5 The value of x with with pre-plusplus = 5 The value of x after the pre-plusplus is = 5
  • 26. Php include Say we wanted to create a common menu file that all our pages will use. A common practice for naming files that are to be included is to use the &quot;.php&quot; extension. Since we want to create a common menu let's save it as &quot;menu.php&quot;. menu.php code: <html> <body> <a href=&quot;http://www.example.com/index.php&quot;>Home</a> - <a href=&quot;http://www.example.com/about.php&quot;>About Us</a> - <a href=&quot;http://www.example.com/links.php&quot;>Links</a> - <a href=&quot;http://www.example.com/contact.php&quot;>Contact Us</a> <br />
  • 27. ...continued index.php Code: <?php include(&quot;menu.php&quot;); ?> <p>This is my home page that uses a common menu to save me time when I add new pages to my website!</p> </body> </html> Display: Home - About Us - Links - Contact Us This is my home page that uses a common menu to save me time when I add new pages to my website!
  • 28. Php forms order.html Code: <html> <body> <h4>Tizag Art Supply Order Form</h4> <form> <select> <option>Paint</option> <option>Brushes</option> <option>Erasers</option></select> Quantity: <input type=&quot;text&quot; /> <input type=&quot;submit&quot; /> </form> </body> </html>
  • 29. PHP Form Processor We want to get the &quot;item&quot; and &quot;quantity&quot; inputs that we have specified in our HTML form. Using an associative array (this term is explained in the array lesson), we can get this information from the $_POST associative array. The proper way to get this information would be to create two new variables, $item and $quantity and set them equal to the values that have been &quot;posted&quot;. The name of this file is &quot;process.php&quot;.
  • 30. process.php Code: <html><body> <?php $quantity = $_POST['quantity']; $item = $_POST['item']; echo &quot;You ordered &quot;. $quantity . &quot; &quot; . $item . &quot;.<br />&quot;; echo &quot;Thank you for ordering from Tizag Art Supplies!&quot;; ?> </body></html> As you probably noticed, the name in $_POST['name'] corresponds to the name that we specified in our HTML form.
  • 31. Now try uploading the &quot;order.html&quot; and &quot;process.php&quot; files to a PHP enabled server and test them out. If someone selected the item brushes and specified a quantity of 6, then the following would be displayed on &quot;process.php&quot;: process.php Code: You ordered 6 brushes. Thank you for ordering from Tizag Art Supplies!
  • 32. Php function PHP Code with Function: <?php function myCompanyMotto(){ echo &quot;We deliver quantity, not quality!<br />&quot;; } echo &quot;Welcome to Tizag.com <br />&quot;; myCompanyMotto(); echo &quot;Well, thanks for stopping by! <br />&quot;; echo &quot;and remember... <br />&quot;; myCompanyMotto(); ?>
  • 33. Display: Welcome to Tizag.com We deliver quantity, not quality! Well, thanks for stopping by! and remember... We deliver quantity, not quality! # Always start your function with the keyword function # Remember that your function's code must be between the &quot;{&quot; and the &quot;}&quot;
  • 34. Php function parameters if we use parameters, then we add some extra functionality! A parameter appears with the parentheses &quot;( )&quot; and looks just like a normal PHP variable. Our parameter will be the person's name and our function will concatenate this name onto a greeting string. PHP Code with Function: <?php function myGreeting($firstName){ echo &quot;Hello there &quot;. $firstName . &quot;!<br />&quot;; } ?>
  • 35. PHP Code: <?php function mySum($numX, $numY){ $total = $numX + $numY; return $total; } $myNumber = 0; echo &quot;Before the function, myNumber = &quot;. $myNumber .&quot;<br/>&quot;; $myNumber = mySum(3, 4); // Store the result of mySum in $myNumber echo &quot;After the function, myNumber = &quot; . $myNumber .&quot;<br/>&quot;; ?> Display: Before the function, myNumber = 0 After the function, myNumber = 7
  • 36. Php files In PHP, a file is created using a command that is also used to open files. It may seem a little confusing, but we'll try to clarify this conundrum. In PHP the fopen function is used to open files. However, it can also create a file if it does not find the file specified in the function call. So if you use fopen on a file that does not exist, it will create it, given that you open the file for writing or appending.
  • 37. Creating files The fopen function needs two important pieces of information to operate correctly. First, we must supply it with the name of the file that we want it to open. Secondly, we must tell the function what we plan on doing with that file (i.e. read from the file, write information, etc). Since we want to create a file, we must supply a file name and tell PHP that we want to write to the file. Note: We have to tell PHP we are writing to the file, otherwise it will not create a new file.
  • 38. PHP Code: $ourFileName = &quot;testFile.txt&quot;; $ourFileHandle = fopen($ourFileName, 'w') or die(&quot;can't open file&quot;); fclose($ourFileHandle); The file &quot;testFile.txt&quot; should be created in the same directory where this PHP code resides. PHP will see that &quot;testFile.txt&quot; does not exist and will create it after running this code. There's a lot of information in those three lines of code, let's make sure you understand it.
  • 39. PHP Sessions When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using PHP sessions. A normal HTML website will not pass data from one page to another. This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next.
  • 40. ...continued Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage. If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.
  • 41. Starting a PHP Session Before you can begin storing user information in your PHP session, you must first start the session. When you start a session, it must be at the very beginning of your code, before any HTML or text is sent. Below is a simple script that you should place at the beginning of your PHP code to start up a PHP session. PHP Code: <?php session_start(); // start up your PHP session! ?> This tiny piece of code will register the user's session with the server, allow you to start saving user information and assign a UID (unique identification number) for that user's session.