PHP Basics Introduction to PHP a PHP file

  • Slides: 25
Download presentation
PHP Basics Introduction to PHP • a PHP file, PHP workings, running PHP. •

PHP Basics Introduction to PHP • a PHP file, PHP workings, running PHP. • Basic PHP syntax • variables, operators, if. . . else. . . and switch, while, do while, and for. Some useful PHP functions How to work with • HTML forms, cookies, files, time and date. How to create a basic checker for user-entered data

What do You Need? ﻣی ﻧﻮیﺴیﺪ PHP ﺷﻤﺎ ﺑﻪ ﻧﺮﻡ ﺍﻓﺰﺍﺭﻫﺎی ﺯیﺮ ﺑﺮﺍی ﺍﺟﺮﺍی

What do You Need? ﻣی ﻧﻮیﺴیﺪ PHP ﺷﻤﺎ ﺑﻪ ﻧﺮﻡ ﺍﻓﺰﺍﺭﻫﺎی ﺯیﺮ ﺑﺮﺍی ﺍﺟﺮﺍی ﻭﺏ ﺳﺎیﺘﻬﺎیی کﻪ ﺑﺎ : ﺩﺍﺭیﺪ PHP ﺯﺑﺎﻥ ﺍﺳکﺮیپﺖ ﻧﻮیﺴی 1 My. SQL –پﺎیگﺎﻩ ﺩﺍﺩﻩ 2 iis ﻭﺏ ﺳﺮﻭﺭ آپﺎچی یﺎ 3 - § Download PHP for free here: http: //www. php. net/downloads. php § Download My. SQL for free here: http: //www. mysql. com/downloads/index. html § Download Apache for free here: http: //httpd. apache. org/download. cgi

switch : ﺷﺮﻁ <html><head></head> <body> <!–- switch-cond. php COMP 519 --> <? php $x

switch : ﺷﺮﻁ <html><head></head> <body> <!–- switch-cond. php COMP 519 --> <? php $x = rand(1, 5); // random integer echo “x = $x <br/>”; switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ? > </body> </html> switch (expression) { case label 1: code to be executed if expression = label 1; break; case label 2: code to be executed if expression = label 2; break; default: code to be executed if expression is different from both label 1 and label 2; } view the output page

do-while ﻭ while : ﺣﻠﻘﻪ <html><head></head> <body> <? php $i=1; while($i <= 5) {

do-while ﻭ while : ﺣﻠﻘﻪ <html><head></head> <body> <? php $i=1; while($i <= 5) { echo "The number is $i "; $i++; } ? > </body> </html> view the output page while ﺣﻠﻘﻪ <html><head></head> <body> <? php $i=0; do { $i++; echo "The number is $i "; } while($i <= 10); ? > </body> </html> view the output page do-while ﺣﻠﻘﻪ

foreach ﻭ for : ﺣﻠﻘﻪ <? php for ($i=1; $i<=5; $i++) { echo "Hello

foreach ﻭ for : ﺣﻠﻘﻪ <? php for ($i=1; $i<=5; $i++) { echo "Hello World! "; } ? > <? php $a_array = array(1, 2, 3, 4); foreach ($a_array as $value) { $value = $value * 2; echo “$value <br/> n”; } ? > یک ﺣﻠﻘﻪ ﺑﺎ ﺗﻌﺪﺍﺩ ﺗکﺮﺍﺭ ﻣﺸﺨﺺ view the output page <? php $a_array=array("a", "b", "c"); foreach ($a_array as $key=>$value) { echo $key. " = ". $value. "n"; } ? > . ﺑﻪ ﺍﻧﺪﺍﺯﻩ ﻋﻨﺎﺻﺮ آﺮﺍیﻪ ﺗکﺮﺍﺭ ﻣی ﺷﻮﺩ

User Defined Functions <? php function foo($arg_1, $arg_2, /*. . . , */ $arg_n)

User Defined Functions <? php function foo($arg_1, $arg_2, /*. . . , */ $arg_n) { echo "Example function. n"; return $retval; } ? > <? php function square($num) { return $num * $num; } echo square(4); ? > <? php function small_numbers() { return array (0, 1, 2); } list ($zero, $one, $two) = small_numbers(); echo $zero, $one, $two; echo "<br/>“ ? > <? php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } takes_array(1, 2)); echo "<br/>“ ? > view the output page

Including Files. یک ﻓﺎیﻞ ﺭﺍ ﺍﻟﺤﺎﻕ ﻭ ﺍﺭﺯیﺎﺑی ﻣی کﻨﺪ include() ﻋﺒﺎﺭﺕ vars. php

Including Files. یک ﻓﺎیﻞ ﺭﺍ ﺍﻟﺤﺎﻕ ﻭ ﺍﺭﺯیﺎﺑی ﻣی کﻨﺪ include() ﻋﺒﺎﺭﺕ vars. php <? php function foo() { global $color; $color = 'green'; $fruit = 'apple'; ? > include ('vars. php‘); test. php <? php echo "A $color $fruit"; } echo "A $color $fruit"; // A include 'vars. php'; /* * vars. php is in the scope of foo() so * $fruit is NOT available outside of this * scope. $color is because we declared it * as global. */ echo "A $color $fruit"; // A green apple foo(); echo "A $color $fruit"; ? > // A green apple // A green ? > view the output page . • ﻣﺤﺪﻭﺩﻩ ﻣﺘﻐییﺮﻫﺎی کﻪ ﺩﺭ ﻓﺎیﻞ ﺍﻟﺤﺎﻕ ﺷﺪﻩ ﺗﻌﺮیﻒ ﺷﺪﻩ ﺍﻧﺪ ﺑﻪ ﻣﺤﻞ ﺍﻟﺤﺎﻕ ﻓﺎیﻞ ﺑﺴﺘگی ﺩﺍﺭﺩ . ﻧیﺰ ﺍﺳﺘﻔﺎﺩﻩ ﻧﻤﻮﺩ require_once ﻭ include_once • ﻣی ﺗﻮﺍﻥ ﺍﺯ ﻋﺒﺎﺭﺗﻬﺎی

PHP Information. ﻧﺼﺐ ﺷﺪﻩ ﺍﺳﺘﻔﺎﺩﻩ ﻣی گﺮﺩﺩ PHP ﺑﺮﺍی ﻧﻤﺎیﺶ ﺍﻃﻼﻋﺎﺕ کﻠی ﻧﺴﺨﻪ phpinfo()

PHP Information. ﻧﺼﺐ ﺷﺪﻩ ﺍﺳﺘﻔﺎﺩﻩ ﻣی گﺮﺩﺩ PHP ﺑﺮﺍی ﻧﻤﺎیﺶ ﺍﻃﻼﻋﺎﺕ کﻠی ﻧﺴﺨﻪ phpinfo() ﺗﺎﺑﻊ <html><head></head> <!– info. php COMP 519 <body> <? php // Show all PHP information phpinfo(); ? > <? php // Show only the general information phpinfo(INFO_GENERAL); ? > </body> </html> view the output page INFO_GENERAL line, The configuration php. ini location, build date, Web Server, System and more INFO_CREDITS PHP 4 credits INFO_CONFIGURATION Local and master values for php directives INFO_MODULES Loaded modules INFO_ENVIRONMENT Environment variable information INFO_VARIABLES variables All predefined from EGPCS INFO_LICENSE PHP license information INFO_ALL Shows all of the above (default)

Server Variables. یک ﻣﺘﻐییﺮ ﺭﺯﺭﻭ ﺷﺪﻩ ﺍﺳﺖ کﻪ ﺗﻤﺎﻡ ﺍﻃﻼﻋﺎﺕ ﺳﺮﻭﺭ ﺍﺯ ﻃﺮیﻖ آﻦ

Server Variables. یک ﻣﺘﻐییﺮ ﺭﺯﺭﻭ ﺷﺪﻩ ﺍﺳﺖ کﻪ ﺗﻤﺎﻡ ﺍﻃﻼﻋﺎﺕ ﺳﺮﻭﺭ ﺍﺯ ﻃﺮیﻖ آﻦ ﻗﺎﺑﻞ ﺩﺳﺘﺮﺳی ﺍﺳﺖ $_SERVER. ﺍیﻦ ﻣﺘﻐییﺮ یک ﻣﺘﻐییﺮ ﻋﻤﻮﻣی ﺍﺳﺖ <html><head></head> <body> <? php echo "Referer: ". $_SERVER["HTTP_REFERER"]. " "; echo "Browser: ". $_SERVER["HTTP_USER_AGENT"]. " "; echo "User's IP address: ". $_SERVER["REMOTE_ADDR"]; ? > </body> </html> view the output page

 کﺎﺭ ﺑﺎ ﻓﺎیﻠﻬﺎ . ﺑﻨﺪﺩ ﻓﺎیﻞ ﺭﺍ ﻣی fclose(). یک کﺎﺭﺍکﺘﺮ ﺍﺯ ﻓﺎیﻞ

کﺎﺭ ﺑﺎ ﻓﺎیﻠﻬﺎ . ﺑﻨﺪﺩ ﻓﺎیﻞ ﺭﺍ ﻣی fclose(). یک کﺎﺭﺍکﺘﺮ ﺍﺯ ﻓﺎیﻞ ﻣی ﺧﻮﺍﻧﺪ fgetc() یک ﺭﺷﺘﻪ ﺭﺍ ﺑﺎ یﺎ fputs ﻭ ﻣﻌﺎﺩﻝ آﻦ fwrite(). ﺩﺭ ﻓﺎیﻞ ﻣی ﻧﻮیﺴﺪ n ﺑﺪﻭﻥ <? php $my. File = "welcome. txt"; if (!($fh=fopen($my. File, 'r'))) exit("Unable to open file. "); while (!feof($fh)) { $x=fgetc($fh); echo $x; } fclose($fh); view the output ? > page <? php $lines = file('welcome. txt'); foreach ($lines as $l_num => $line) { echo "Line #{$l_num}: ". $line. "<br/>"; view the output } ? > page . ﺍﺳﺖ true ﺍگﺮ ﺩﺭ آﺨﺮ ﻓﺎیﻞ ﺑﺎﺷیﻢ feof() • . یک ﺧﻂ ﺍﺯ ﻓﺎیﻞ ﺭﺍ ﻣی ﺧﻮﺍﻧﺪ fgets() • . کﻞ ﻓﺎیﻞ ﺭﺍ ﺩﺍﺧﻞ یک آﺮﺍیﻪ ﻣی ﺭیﺰﺩ file() • <? php $my. File = "welcome. txt"; $fh = fopen($my. File, 'r'); $the. Data = fgets($fh); fclose($fh); echo $the. Data; view the output ? > page <? php $my. File = "test. File. txt"; $fh = fopen($my. File, 'a') or die("can't open file"); $string. Data = "New Stuff 1n"; fwrite($fh, $string. Data); $string. Data = "New Stuff 2n"; fwrite($fh, $string. Data); fclose($fh); view the output ? > page

 کﺎﺭ کﺮﺩﻥ ﺑﺎ ﻓﺮﻣﻬﺎ <html> <-- form. html COMP 519 --> <body> <form

کﺎﺭ کﺮﺩﻥ ﺑﺎ ﻓﺮﻣﻬﺎ <html> <-- form. html COMP 519 --> <body> <form action="welcome. php" method="POST"> Enter your name: <input type="text" name="name" /> <br/> Enter your age: <input type="text" name="age" /> <br/> <input type="submit" /> <input type="reset" /> </form> </body> </html> <!–- welcome. php COMP 519 --> <body> Welcome <? php echo $_POST["name"]. ”. ”; ? > You are <? php echo $_POST["age"]; ? > years old! </body> </html> view the output page $_POST contains all POST data. $_GET contains all GET data.

Required Fields in User-Entered Data ﺳپﺲ ﺑﺮﻧﺎﻣﻪ چک ﻣی کﻨﺪ. ﺩﺭ ﺍیﻦ ﻣﺜﺎﻝ ﺍﺯ

Required Fields in User-Entered Data ﺳپﺲ ﺑﺮﻧﺎﻣﻪ چک ﻣی کﻨﺪ. ﺩﺭ ﺍیﻦ ﻣﺜﺎﻝ ﺍﺯ کﺎﺭﺑﺮ ﺍﻃﻼﻋﺎﺗی گﺮﻓﺘﻪ ﻣی ﺷﻮﺩ کﻪ کﺎﺭﺑﺮ ﻗﺴﻤﺘﻬﺎی ﻻﺯﻡ ﺭﺍ پﺮ کﺮﺩﻩ ﺍﺳﺖ یﺎ ﻧﻪ؟ <html> <!-- form_checker. php COMP 519 --> <head> <title></title> </head> <body> <? php /*declare some functions*/ Print Function function print_form($f_name, $l_name, $email, $os) { ? > <form action="form_checker. php" method=“POST"> First Name: <input type="text" name=" f_name" value="<? php echo $f_name? >“ /> <br/> Last Name <b>*</b>: <input type="text" name=" l_name" value="<? php echo $l_name? >“ /> <br/> Email Address <b>*</b>: <input type="text" name="email" value=" <? php echo $email? >“ /> <br/> Operating System: <input type="text" name=" os" value="<? php echo $os? >“ /> <br/> <input type="submit" name="submit" value="Submit“ /> <input type=“reset“ /> </form> <? php }

Check and Confirm Functions function check_form($f_name, $l_name, $email, $os) { if (!$l_name||!$email){ echo "<h

Check and Confirm Functions function check_form($f_name, $l_name, $email, $os) { if (!$l_name||!$email){ echo "<h 3>You are missing some required fields!</h 3>"; print_form($f_name, $l_name, $email, $os); } else{ confirm_form($f_name, $l_name, $email, $os); } } function confirm_form($f_name, $l_name, $email, $os) { ? > <h 2>Thanks! Below is the information you have sent to us. </h 2> <h 3>Contact Info</h 3> <? php echo "Name: $f_name $l_name <br/>"; echo "Email: $email <br/>"; echo "OS: $os"; }

Main Program /*Main Program*/ if (!$_POST["submit"]) { ? > <h 3>Please enter your information</h

Main Program /*Main Program*/ if (!$_POST["submit"]) { ? > <h 3>Please enter your information</h 3> <p>Fields with a "<b>*</b>" are required. </p> <? php print_form("", "", ""); } else{ check_form($_POST["f_name"], $_POST["l_name"], $_POST["email"], $_POST["os"]); } ? > </body> </html> view the output page