You are on page 1of 8

PHP Introduction to PHP

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

Defining the site(Dreamweaver)


From the site menu click on new site Type the name of your site Click in next button Select yes, I want to use a server technology Select php mysql option from dropdown box Click in next button Select third option and type the location of your site folder As As c:\apache\htdocs\folder_name (For Local Computer) \\fortunec01\htdocs\folder_name (For server)

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

Basic PHP Syntax


A PHP file normally contains HTML tags, just like an HTML file, and some PHP scripting code. 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.

Example:<html> <body> <?php echo Hello World; ?> </body> </html>


Or

<? echo Hello World; ?>

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

Conditional Statements If Statement


If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement. Syntax if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; } Example:<?php $x=10; if($x==10) { echo "Hello<br />"; echo "Good morning<br />"; } ?>

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

A. The while Statement


The while statement will execute a block of code if and as long as a condition is true. Syntax while(condition) code to be executed; Example The following example demonstrates a loop that will continue to run as long as the variable i is less than, or equal to 5. i will increase by 1 each time the loop runs: <html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html>

B. 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 The following example will increment the value of i at least once, and it will continue incrementing the variable i while it has a value of less than 5: <html> <body> <?php $i=0; do { $i++; echo "The number is " . $i . "<br />";

} while ($i<5); ?> </body> </html>

C. 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; } Note: The for statement has three parameters. The first parameter is for initializing variables, the second parameter holds the condition, and the third parameter contains any increments required to implement the loop. If more than one variable is included in either the initialization or the increment section, then they should be separated by commas. The condition must evaluate to true or false. Example The following example prints the text "Hello World!" five times: <html> <body> <?php for($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } ?> </body> </html>

D. The foreach Statement


Loops over the array given by the parameter. On each loop, the value of the current element is assigned to $value and the array pointer is advanced by one - so on the next loop, you'll be looking at the next element. Syntax foreach (array as value) { code to be executed; } Example The following example demonstrates a loop that will print the values of the given array: <html> <body>

<?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html>

Connecting to a MySQL Database


In PHP, this is done with the mysql_connect() function. Syntax:mysql_connect(servername,username,password);

Parameter servername username password

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

?>

Working with Database

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"; ?>

You might also like