Sie sind auf Seite 1von 8

PHP

(Personal Home Page) Hypertext Preprocessor What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP. PHP scripts are executed on the server PHP supports databases like MySQL PHP is an open source software (OSS) PHP is free to download and use PHP files may contain text, HTML tags and scripts.
Page 1 of 8

PHP

PHP files are returned to the browser as plain HTML. PHP files have a file extension of ".php".
Why PHP? PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: http://www.php.net/ PHP is easy to learn and runs efficiently on the server side. Basic PHP Syntax A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. Below, we have an example of a simple PHP script which sends the text "Hello World" to the browser: <?php echo "Hello World"; ?> Note:A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document. Each code line in PHP must end with a semicolon. The semicolon is a separator and is used to distinguish one set of instructions from another. There are two basic statements to output text with PHP: echo and print. In the example above we have used the echo statement to output the text "Hello World". Variables in PHP All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays. Below, the PHP script assigns the string "Hello World" to a variable called $txt: <?php $txt="Hello World"; echo $txt; ?> To concatenate two or more variables together, use the dot (.) operator: <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2 ; ?> The output of the script above will be: "Hello World 1234".
Page 2 of 8

PHP
Comments in PHP In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <html> <body> <?php//This is a comment /*This isa comment block*/ ?> </body> </html> OPERATORS Arithmetic Operators:Operator + * / % ++ -Operation Addition Subtraction Multiplication Division Modulus (division remainder) Increment Decrement Example x=2 x+2 x=2 5-x x=4 x*5 15/5 5/2 5%2 10%8 0%2 x=5 x++ x=5 x-Result 4 3 20 3 2.5 1 2 0 x=6 x=4

Assignment Operators:Operator = += -= *= /= %= Comparison Operators:Page 3 of 8

Example x=y x+=y x-=y x*=y x/=y x%=y

Is The Same As x=y x=x+y x=x-y x=x*y x=x/y x=x%y

PHP
Operator == != > < >= <= Logical Operators:Operator && Description and Example x=6 y=3 (x < 10 && y > 1) returns true x=6 y=3 (x==5 || y==5) returns false x=6 y=3 !(x==y) returns true Description is equal to is not equal is greater than is less than is greater than or equal to is less than or equal to Example 5==8 returns false 5!=8 returns true 5>8 returns false 5<8 returns true 5>=8 returns false 5<=8 returns true

||

or

not

LOOPING STATEMENTS Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this. In PHP we have the following looping statements: The for Statement:The for statement is used when you know how many times you want to execute a statement or a list of statements. Syntax for (initialization; condition; increment) { code to be executed; } Example: <html><head> <body background="Z:\scene\1monument (Large).jpg"> <?php for($i=1; $i<=10; $i++) { echo "<h3> <font color=green>APTECH COMPUTER EDUCATION<BR>"; } ?> </body></head></html> The while Statement
Page 4 of 8

PHP
The while statement will execute a block of code if and as long a condition is true. Syntax while (condition) code to be executed; Example <?php $a=1; while($a<=5) { echo "<h2><fontcolor=blue>The number is:".$a."<br>"; $a++; } ?> The do...while Statement The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. Syntax Do { code to be executed; } while (condition); Example <?php $a=0; do { $a++; echo "<h3>The number is:".$a."<br>"; } while ($a<10); ?> CONDITIONAL STATEMENTS Very often hen you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do this. In PHP we have the following conditional statements: 1.) If statement:-This conditional statement is used to check a single action,i.e( the true action). If the condition is true then it will give the result otherwise it will not. Example :<?php $d=date('D'); if($d=="sun") { echo "<h1>Have a nice day"; } ?>
Page 5 of 8

PHP
2.) Ifelse:- This conditional statement is used to check two actions. If the condition is true it will give true and if the condition is false it will give false. Example :<?php $d=date('D'); if($d=="Fri") { echo "<h1>Have a nice Friday"; } else { echo "<h1>Have a nice day</h1>"; } ?> 3.) If ..else if.else if..else :- This control statement is used to compare one value with different conditions and all the conditions having their own actions. Example:<?php $day="sun"; if($day==sun) { echo"Today is sunday"; } else if($day==mon) { echo"Today is monday"; } else if($day==tue) { echo"Today is tuesday"; } else if($day==wed) { echo"Today is wednesday"; } else if($day==thu) { echo"Today is thursday"; } else if($day==fri) { echo"Today is friday"; } else { echo"today is saturday"; } ?>
Page 6 of 8

PHP
The Switch Statement If you want to select one of many blocks of code to be executed, use the Switch statement. Syntax 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; } <?php $day=0; switch($day) { case 1: {echo "Sunday"; break;} case 2: {echo "Monday"; Break;} case 3: {echo "Tuesday"; break;} case 4: {echo "Wednesday"; break;} case 5: {echo "Thursday"; break;} default: {echo "Saturday";} } ?> PHP MySQL Connection Code For Displaying Records <?php $con=mysql_connect("fortunec01","root",""); $db=mysql_select_db("Anup_Batch",$con) or die(mysql_error()); $rs=mysql_query("select * from morning_batch"); echo("<h1>Records present are:</h1>"); echo("<table border=2>"); echo("<tr><th>Roll no.</th> <th>Student's Name</th> <th>Gender</th> <th>Address</th> <th>Email</th> </tr>");
Page 7 of 8

PHP
while ($rows=mysql_fetch_array($rs)) { echo("<tr><td>".$rows[0]. "</td><td>".$rows[1]. "</td><td>".$rows[2]. "</td><td>".$rows[3]. "</td><td>".$rows[4]. "</td></tr>"); } ?> For inserting Records <?php $Roll=$_POST["txtRoll"]; $stName=$_POST["txtStudent"]; $Gender=$_POST["txtGender"]; $Address=$_POST["txtAddress"]; $Email=$_POST["txtEmail"]; $con=mysql_connect("localhost","","") or die(mysql_error()); $db=mysql_select_db("Anup_Batch",$con) or die(mysql_error()); mysql_query("insert into Morning_Batch values('".$Roll."','".$stName."','".$Gender."','".$Address."','".$Email."')",$con) or die(mysql_error()); //mysql_query("insert into Morning_Batch values('$Roll','$stName','$Gender','$Address','$Email'"),$con) or die(mysql_error()); echo "Record inserted successfully"; ?> For Updating Records <?php $Roll=$_POST["txtRoll"]; $stName=$_POST["txtStudent"]; $Gender=$_POST["txtGender"]; $Address=$_POST["txtAddress"]; $Email=$_POST["txtEmail"]; $con=mysql_connect("localhost","","") or die(mysql_error()); $db=mysql_select_db("Anup_Batch",$con) or die(mysql_error()); mysql_query("update morning_batch set Roll='$Roll',Stname='$stName',Gender='$Gender',Address='$Address',Email='$Email' where Roll='$Roll'",$con) or die (mysql_error()); echo "Your record has been updated successfully"; ?> For Deleting Records <?php $del=$_POST["txtRoll"]; $con=mysql_connect("localhost","","") or die(mysql_error()); $db=mysql_select_db("Anup_Batch",$con) or die(mysql_error()); mysql_query("delete from Morning_Batch where Roll='".$del."'") or die(mysql_error()); echo "Record has been deleted successfully"; ?>
Page 8 of 8

Das könnte Ihnen auch gefallen