Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 PHP در سال 1995 توسط راسموس لِردورت ابداع شد.  در اصل برای ردیابی مشاهده کنندگان وب سایت لِردورت درست شد.  بعد از دو سال به همراه آپاچی یکی از بهترین ابزارهای تولید صفحات دینامیک شد.  یک زبان اسکریپتی رایگان و با کد باز است.  کاملا با mySQL سازگار است. PHP مثل جاوا اسکریپت است منتهی در سمت سرور کار می کند.  یعنی با برچسپهای مخصوص و داخل کد HTML جاسازی می شود.  از روی نوع فایل (.php یا.phtml) سرور متوجه می شود با متن PHP سروکار دارد.  سرور کد PHP را اجرا و نتیجه را داخل صفحه HTML قرار می دهد.  لذا کاربر هیچوقت کد PHP را نمی بیند و فقط نتیجه اجرا را می بیند.

3 What do You Need? شما به نرم افزارهای زیر برای اجرای وب سایتهایی که با PHP می نویسید دارید : 1 - زبان اسکریپت نویسی PHP 2 – پایگاه داده MySQL 3 - وب سرور آپاچی یا iis  Download PHP for free here: http://www.php.net/downloads.php  Download MySQL for free here: http://www.mysql.com/downloads/index.html  Download Apache for free here: http://httpd.apache.org/download.cgi

4 Basic PHP syntax هر قطعه کد PHP بین برچسپهای قرار می گیرد.  دستورات print و echo برای نمایش خروجی  هر دستور باید با ; خاتمه یابد غیر از آخرین دستور که وجود ; اختیاری است.  // برای توضیحات تک خطی  /* و */ برای توضیحات چند خطی Hello World This is going to be ignored by the PHP interpreter. While this is going to be parsed. ‘; ?> This will also be ignored by PHP. Hello and welcome to my page! '); ?> <?php //This is a comment /* This is a comment block */ ?> view the output page

5 متغییر های اسکالر انواع متغییرها:  boolean  integer  float  string برای نگهداری رشته ها <?php $foo = True; if ($foo) echo "It is TRUE! \n"; $txt='1234'; echo "$txt \n"; $a = 1234; echo "$a \n"; $a = -123; echo "$a \n"; $a = 1.234; echo "$a \n"; $a = 1.2e3; echo "$a \n"; $a = 7E-10; echo "$a \n"; echo 'Arnold once said: "I\'ll be back"', " \n"; $beer = 'Heineken'; echo "$beer's taste is great \n"; $str = <<<EOD Example of string spanning multiple lines using “heredoc” syntax. EOD; echo $str; ?> view the output page متغییر با علامت $ شروع می شوند و می توانند از رشته ها و اعداد نگهداری کنند. نوع یک متغییر وابسته به داده ای است که نگهداری میکند و در حین اجرا قابل تغییر است.

6 آرایه آرایه ها در PHP بصورت نقشه وجود دارند. یعنی هر آیتم دارای کلید است. دستور array() آرایه را ایجاد می کنذ. <?php $arr = array("foo" => "bar", 12 => true); echo $arr["foo"]; // bar echo $arr[12]; // 1 ?> key یک عدد یا یک رشته است. value هر نوع دادهای معتبر در PHP <?php array(5 => 43, 32, 56, "b" => 12); array(5 => 43, 6 => 32, 7 => 56, "b" => 12); ?> اگر ایتمی دارای کلید نباشد، بزرگترین کلید + 1 به عنوان کلید آن قرار داده می شود. <?php $arr = array(5 => 1, 12 => 2); $arr[] = 56; // the same as $arr[13] = 56; $arr["x"] = 42; // adds a new element unset($arr[5]); // removes the element unset($arr); // deletes the whole array $a = array(1 => 'one', 2 => 'two', 3 => 'three'); unset($a[2]); $b = array_values($a); ?> unset() یک زوج کلید / مقدار را حذف می کند. array_values() آرایه را بصورت شمارشی ایندکس گذاری می کند. view the output page

7 یک ثابت اسمی است که به یک مقدار نسبت داده می شود. برای اسم گذاری ثابتها معمولا از حروف بزرگ استفاده می شود. ثابتها را می شود در هر جای برنامه استفاده کرد. <?php // Valid constant names define("FOO", "something"); define("FOO2", "something else"); define("FOO_BAR", "something more"); // Invalid constant names define("2FOO", "something"); // This is valid, but should be avoided: // PHP may one day provide a “magical” constant // that will break your script define("__FOO__", "something"); ?> ثابتها

8 عملگرها عملگرهای ریاضی : +, -, *,/, %, ++, -- عملگرهای انتساب : : =, +=, -=, *=, /=, %= عملگرهای مقایسه ای : ==, !=, >, =, <= عملگرهای منطقی : &&, ||, ! عملگرهای رشته ها :.,.= Example Is the same as x+=y x=x+y x-=y x=x-y x*=y x=x*y x/=y x=x/y x%=y x=x%y $a = "Hello "; $b = $a. "World!"; // now $b contains "Hello World!" $a = "Hello "; $a.= "World!";

9 شرط : if else <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend! "; else echo "Have a nice day! "; $x=10; if ($x==10) { echo "Hello "; echo "Good morning "; } ?> if (condition) code to be executed if condition is true; else code to be executed if condition is false; view the output page date() یک تابع آماده است که می توان آنرا با پارامترهای مختلف صدا زد تا تاریخ و زمان را با فرمتهای متفاوت برگرداند. در این حالت ما امروز را بصورت یک رشته سه حرفی گرفته ایم.

10 شرط : switch <?php $x = rand(1,5); // random integer echo “x = $x ”; 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"; } ?> switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; } view the output page

11 حلقه : while و do-while <?php $i=1; while($i <= 5) { echo "The number is $i "; $i++; } ?> حلقه while view the output page <?php $i=0; do { $i++; echo "The number is $i "; } while($i <= 10); ?> حلقه do-while view the output page

12 حلقه : for و foreach <?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 \n”; } ?> به اندازه عناصر آرایه تکرار می شود. <?php $a_array=array("a","b","c"); foreach ($a_array as $key=>$value) { echo $key." = ".$value."\n"; } ?> view the output page

13 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 " “ ?> <?php function takes_array($input) { echo "$input[0] + $input[1] = ", $input[0]+$input[1]; } takes_array(array(1,2)); echo " “ ?> view the output page

14 Variable Scope <?php $a = 1; /* global scope */ function Test() { echo $a; /* reference to local scope variable */ } Test(); echo " “ ?> داخل توابع متغییر ها ارزش محلی دارند. <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; echo " " ?> global برای متغییرهای عمومی <?php function Test1() { static $a = 0; echo $a; $a++; } Test1(); echo " " ?> static برای متغییرهایی که مقدار خود را از دست نمی دهند. view the output page

15 Including Files عبارت include() یک فایل را الحاق و ارزیابی می کند. vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> محدوده متغییرهای که در فایل الحاق شده تعریف شده اند به محل الحاق فایل بستگی دارد. می توان از عبارتهای include_once و require_once نیز استفاده نمود. view the output page <?php function foo() { global $color; include ('vars.php‘); echo "A $color $fruit"; } /* 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. */ foo(); // A green apple echo "A $color $fruit"; // A green ?> view the output page

16 PHP Information تابع phpinfo() برای نمایش اطلاعات کلی نسخه PHP نصب شده استفاده می گردد. <!– info.php COMP519 <?php // Show all PHP information phpinfo(); ?> <?php // Show only the general information phpinfo(INFO_GENERAL); ?> INFO_GENERALThe configuration line, php.ini location, build date, Web Server, System and more INFO_CREDITSPHP 4 credits INFO_CONFIGURATIONLocal and master values for php directives INFO_MODULESLoaded modules INFO_ENVIRONMENTEnvironment variable information INFO_VARIABLESAll predefined variables from EGPCS INFO_LICENSEPHP license information INFO_ALLShows all of the above (default) view the output page

17 Server Variables $_SERVER یک متغییر رزرو شده است که تمام اطلاعات سرور از طریق آن قابل دسترسی است. این متغییر یک متغییر عمومی است. <?php echo "Referer: ". $_SERVER["HTTP_REFERER"]. " "; echo "Browser: ". $_SERVER["HTTP_USER_AGENT"]. " "; echo "User's IP address: ". $_SERVER["REMOTE_ADDR"]; ?> view the output page

18 File Open تابع fopen("file_name","mode") برای باز کردن فایل استفاده می شود. <?php $fh=fopen("welcome.txt","r"); ?> rRead only. r+Read/Write. wWrite only. w+Read/Write. aAppend. a+Read/Append. xCreate and open for write only. x+Create and open for read/write. <?php if (!($fh=fopen("welcome.txt","r"))) exit("Unable to open file!"); ?> در حالتهای w, و a, اگر فایل وجود نداشته باشد، fopen سعی می کند فایل را ایجاد کند. در حالت x ، اگر فایل وجود داشته باشد، منجر به تولید خطا میگردد. اگر فایل مورد نظر باز نشود عدد صفر بر می گردد.

19 کار با فایلها fclose() فایل را می بندد. fgetc() یک کاراکتر از فایل می خواند. fwrite () و fputs یک رشته را با و بدون \n در فایل می نویسد. <?php $myFile = "welcome.txt"; if (!($fh=fopen($myFile,'r'))) exit("Unable to open file."); while (!feof($fh)) { $x=fgetc($fh); echo $x; } fclose($fh); ?> <?php $myFile = "welcome.txt"; $fh = fopen($myFile, 'r'); $theData = fgets($fh); fclose($fh); echo $theData; ?> <?php $myFile = "testFile.txt"; $fh = fopen($myFile, 'a') or die("can't open file"); $stringData = "New Stuff 1\n"; fwrite($fh, $stringData); $stringData = "New Stuff 2\n"; fwrite($fh, $stringData); fclose($fh); ?> <?php $lines = file('welcome.txt'); foreach ($lines as $l_num => $line) { echo "Line #{$l_num}:".$line. " "; } ?> view the output page feof() اگر در آخر فایل باشیم true است. fgets() یک خط از فایل را می خواند. file() کل فایل را داخل یک آرایه می ریزد.

20 کار کردن با فرمها Enter your name: Enter your age: Welcome You are years old! $_POST contains all POST data. $_GET contains all GET data. view the output page

21 کار با کوکیها تابع setcookie(name,value,expire,path,domain) برای ایجاد کوکی استفاده می گردد. <?php setcookie("uname", $_POST["name"], time()+36000); ?> Dear, a cookie was set on this page! The cookie will be active when the client has sent the cookie back to the server. نکته : تابع setcookie() باید قبل از برچسپ قرار بگیرد. view the output page <?php if (isset($_COOKIE["uname"])) echo "Welcome ". $_COOKIE["uname"]. "! "; else echo "You are not logged in! "; ?> متغییر $_COOKIE تمام اطلاعات کوکی را دارد. تابع isset() یک کوکی را چک می کند تا ببیند تعریف شده است یا نه؟ اسم متغییر به عنوان پارامتر به تابع فرستاده می شود. view the output page

22 نمایش زمان و تاریخ date() و time() برای نمایش زمان و تاریخ استفاده میگردد. <?php //Prints something like: Monday echo date("l"); echo " “; //Like: Monday 15th of January 2003 05:51:38 AM echo date("l dS \of F Y h:i:s A"); echo " “; //Like: Monday the 15th echo date("l \\t\h\e jS"); ?> تابع date() یک رشته با فرمتی که از طریق پارامتر اول فرستاده می گردد را بر می کرداند. <?php echo " “; $nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs echo 'Now: '. date('Y-m-d')."\n"; echo " “; echo 'Next Week: '. date('Y-m-d', $nextWeek)."\n"; ?> time() زمان سیستم را به فرمت GMT برمی گرداند. view the output page

23 Required Fields in User-Entered Data در این مثال از کاربر اطلاعاتی اطلاعاتی گرفته می شود. سپس برنامه چک می کند که کاربر قسمتهای لازم را پر کرده است یا نه؟ <?php /*declare some functions*/ function print_form($f_name, $l_name, $email, $os) { ?> First Name: “ /> Last Name * : “ /> Email Address * : “ /> Operating System: “ /> <?php } Print Function

24 Check and Confirm Functions function check_form($f_name, $l_name, $email, $os) { if (!$l_name||!$email){ echo " You are missing some required fields! "; 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) { ?> Thanks! Below is the information you have sent to us. Contact Info <?php echo "Name: $f_name $l_name "; echo "Email: $email "; echo "OS: $os"; }

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


Download ppt "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."

Similar presentations


Ads by Google