Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. PHP an introduction
  2. 2. What is PHP? • PHP stands for PHP: Hypertext Preprocessor • PHP is a server-side scripting language, which could be imbedded to HTML • PHP scripts are executed on the server • PHP supports many databases • PHP is an open source software, free to download and use 2
  3. 3. PHP Syntax <?php …. ?> <html> <body> <?php echo “Hello World”; ?> </body> </html> 3
  4. 4. Text Editors• Dreamweaver Open as php and add code in code view. Use: Insert > php.• Windows Notepad Make sure your files are saved with the .php extension.• Microsoft Word is not optimal for editing PHP files. If you use it, you must save the file as plain text. 4
  5. 5. .html or .php: where**Inline** **html code with echo and print*<html> <?php <head> <title>HTML with PHP</title> echo "<html>"; </head> echo "<title>HTML with PHP</title>"; <body> echo "<h1>My example</h1>"; <h1>My Example</h1> //your php code here. <?php //your php code here print "<i>Print works too!</i>"; ?> echo “</html>”; <b>Here is some more html.</b> <?php ?> //your php code here ?> </body></html> 5
  6. 6. .html or .php?? • Open new > Page type: php • Save as .php 6
  7. 7. Variables• Variables are used for storing a values, like text strings, numbers or arrays.• When a variable is declared, it can be used over and over again in your script.• All variables in PHP start with a $ sign symbol.• The correct way of declaring a variable in PHP: $name = value; 7
  8. 8. Naming Rules for Variables • A variable name must start with a letter or an underscore “_” • A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString) 8
  9. 9. The Concatenation OperatorThe concatenation operator . is used to put two string together. <?php $txt1=“Hello World!”; $txt2=“What a nice day!”; echo $txt1 . “ ” . $txt2; ?> 9
  10. 10. Comments • Comments can be added to explain the PHP. • Single line comments start with //. • Multi line comments start with /* and end with */ 10
  11. 11. if…else Syntax <?php if (condition1) {code to be executed if condition1 is true}; elseif (condition2) {code to be executed if condition2 is true}; else {code to be executed if condition1 and condition2 are not true}; ?> 11
  12. 12. if…else Example<?php $num1=10; $num2=20; if ($num1>$num2) echo “The first number is greater than the second.<br />”; elseif ($num1<$num2) {echo “The first number is smaller than the second.<br />”; echo “too bad”; } else echo “The first number is equal to the second.<br />”;?> 12
  13. 13. For Loop Syntax The for loop is used when you know in advance how many times the script should run. for (initial condition; end condition; increment) { code to be executed; } 13
  14. 14. For Loop Example <?php for ($i=0; $i<=5; $i++) { echo "The number is " . $i; echo “.<br />”; } ?> 14
  15. 15. While Loop Syntax Initial variable; while (end variable) { code to be executed; variable increment; } 15
  16. 16. While Loop Example <php? $i=0; while ($i<=5) { echo "The number is " . $i . “.<br />”; $i++; } ?> 16
  17. 17. Function Syntax • need to be called in order to execute • Can be called multiple times with different variables function funname(var1, var2) { function code; } funname(var1, var2); 17
  18. 18. Function Example <?php function getGreeting($user){ return “Hello, $user!”; } echo getGreeting(„Toby‟); ?> 18
  19. 19. Function Example function caltax($num1) {$tax=0.0875; $total = $num1+($num1*$tax); echo "The total is $” . $total . ". <br />”; } caltax(100); 19
  20. 20. Works CitedMost of the information on this PowerPoint are basedon W3Schools Online Web Tutorials PHP section. 20