Documente Academic
Documente Profesional
Documente Cultură
A PHP is a personal Home Page by Rasman Lerdorf (Originally). PHP is now officially known as PHP: Hypertext preprocessor. A PHP file may contain text, HTML tags and scripts. Scripts in a PHP file are executed on the server. It is server-side scripting language usually written in an HTML context. Unlike an ordinary HTML page, a PHP script is not send directly to a client by the server; instead, it is parsed by the PHP binary or module.HTML elements in the script can query database, create images, read and write files, talk to remove servers-the possibilities are endless. The output from PHP code is combined with the HTML in the script and the result send to the user.
Feature of 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: www.php.net PHP is easy to learn and runs efficiently on the server side Encrypt Data Use PHP user authentication to restrict access to sections of your Web site Perform system functions: create, open, read from, write to, and close files on your system; execute system commands; create directories; and modify permissions
Click on next Type your folder name As http://localhost/folder_name (For local Computer) As http://fortunec01/folder_name (For local Computer) Click on next Click on Done
Note:- 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
Variable names begin with the dollar sign ($) and are followed by a concise, meaningful name. The variable name cannot begin with a numeric character, but it can contain numbers and the underscore character (_). Additionally, variable names are case sensitive, meaning that $NAME and $name are two different variables. Variables may contain strings, numbers, or arrays. There are three types of variables: Integers: It contains whole numbers (numbers without decimals). E.g. 12345. You can use either octal or hexadecimal notation: the octal 0123 is decimal 83 and hexadecimal 0x12 is decimal 18. Floating-point numbers: Numbers with decimals. E.g. 1.5, 55.1258 etc. Strings: Text and/or numeric information, specified within double quotes () or single quotes (). <?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;
Example:-
?>
Comments
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <?php //This is a comment /* This is a comment block */ ?>
Operators
A. Arithmetic Operators B. Assignment Operators C. Comparison Operators D. Logical Operators
switch statement
we use this statement if you want to select one of many sets of lines to execute. Use break to prevent the code from running into the next case automatically. The default statement is used if none of the cases are true.
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 label2; } Example:<?php $a=2; switch($a) { case 1: { echo "sunday"; } break; case 2: { echo "Monday"; } break; default: { echo "Invalid Entry"; } } ?>
and
Looping
Looping statements in PHP are used to execute the same block of code a specified number of times *0 while - loops through a block of code if and as long as a specified condition is true *1 do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true *2 for - loops through a block of code a specified number of times *3 foreach - loops through a block of code for each element in an array
<?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html>
Description Optional. Specifies the server to connect to. Default value is "localhost:3306" Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process Optional. Specifies the password to log in with. Default is ""
In the following example we store the connection in a variable ($con) for later use in the script. The "die" part will be executed if the connection fails:
<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code
?>
Inserting record:
<?php $id=$_POST["txtm_id"]; $fname=$_POST["txtfname"]; $lname=$_POST["txtlname"]; $age=$_POST["txtage"];
$gender=$_POST["txtgender"]; $con=mysql_connect("localhost","","") or die (mysql_error()); $db=mysql_select_db("hello",$con) or die(mysql_error()); mysql_query("insert into member values('$id','$fname','$lname','$age','$gender')",$con) or die(mysql_error()); echo "<h1 align=center> Record Address sucessfully"; ?>
Updating Record:<?php $id=$_POST["txtm_id"]; $fname=$_POST["txtfname"]; $lname=$_POST["txtlname"]; $age=$_POST["txtage"]; $gender=$_POST["txtgender"]; $con=mysql_connect("localhost","","") or die(mysql_error()); $db=mysql_select_db("hello",$con) or die(mysql_error()); $sql=mysql_query("update member set m_id='$id',firstname='$fname',lastname='$lname',age='$age',gender='$gender' where m_id='$id'",$con) or die (mysql_error()); echo "<h1><center>Your Record has been Updated Sucessfully</h3>"; ?>
Show Record:<?php $con=mysql_connect("localhost","","") or die(mysql_error()); $db=mysql_select_db("hello",$con) or die (mysql_error()); $rs=mysql_query("select * from member") or die (mysql_error()); echo "<table border=1 align=center>"; echo "<tr><th>m_id</th><th>firstname</th><th>lastname</th><th>age</th><th>gender</th></tr>"; while($rows=mysql_fetch_array($rs)) { echo "<tr><td>".$rows[0]."</td>"; echo "<td>".$rows[1]."</td>"; echo "<td>".$rows[2]."</td>"; echo "<td>".$rows[3]."</td>"; echo "<td>".$rows[4]."</td>"; echo "<td>".$rows[5]."</td>"; } echo "</table>"; ?>
Delete Record:<?php $del=$_POST["txtdelete"]; $con=mysql_connect("localhost","","")or die (mysql_error()); $db=mysql_select_db("hello") or die(mysql_error()); $del=mysql_query("delete from member where m_id='$del'"); echo "<h1 align=center><font color='red'>Record has been Deleted Successfully"; ?>