php Hypertext Preprocessor Mr. Amit Patel Dept. of I.T..com.com
PHP files can contain text, HTML, JavaScript code, and PHP code PHP code are executed on the server, and the result is returned to the browser as plain HTML What is a PHP File? PHP files have a default file extension of ".php".com
What Can PHP Do? PHP can generate dynamic page content. PHP can create, open, read, write, and close files on the server. PHP can collect form data. PHP can send and receive cookies. PHP can add, delete, modify data in your database. PHP can restrict users to access some pages on your website. PHP can encrypt data. With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML..com
Why PHP? PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP has support for a wide range of databases PHP is free. Download it from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side.com
<html> <body> <h1>my first PHP page</h1> <?php echo Welcome Alpha!";?> </body> </html> Basic PHP Syntax A PHP script starts with <?php and ends with?>:.com
<!DOCTYPE html> <html> <body> Comment in PHP <?php //This is a PHP comment line /* This isa PHP comment block*/?> </body> </html>.com
Rules for Variables in PHP As with algebra, PHP variables can be used to hold values (x=5) or expressions (z=x+y). Variable can have short names (like x and y) or more descriptive names (age, carname, totalvolume). Rules for PHP variables: A variable starts with the $ sign, followed by the name of the variable A variable name must begin with a letter or the underscore character A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) A variable name should not contain spaces Variable names are case sensitive ($y and $Y are two different variables).com
<?php $x=5; $y=6; $z=$x+$y; echo $z;?> x=5 y=6 z=x+y Variables in PHP In algebra we use letters (like x) to hold values (like 5). From the expression z=x+y above, we can calculate the value of z to be 11. In PHP these letters are called VARIABLES..com
PHP is a Loosely Typed Language $txt="hello world!"; $x=5; PHP automatically converts the variable to the correct data type, depending on its value. In a strongly typed programming language, we will have to declare (define) the type and name of the variable before using it..com
PHP Variable Scopes PHP has four different variable scopes: Local scope Global scope Static scope Parameter scope.com
<?php $x=5; // global scope Local Scopes function mytest() echo $x; // local scope mytest(); echo $x;?>.com
Global Scopes <?php $x=5; // global scope $y=10; // global scope function mytest() global $x,$y; $y=$x+$y; mytest(); echo $y; // output is 15?>.com
<?php function mytest() static $x=0; echo $x; $x++; mytest(); mytest(); mytest();?> Static Scopes.com
<?php function mytest($x) echo $x; mytest(5);?> Parameter Scopes A parameter is a local variable whose value is passed to the function by the calling code. Parameters are declared in a parameter list as part of the function declaration:.com
String Variables in PHP String variables are used for values that contain characters. After we have created a string variable we can manipulate it. A string can be used directly in a function or it can be stored in a variable. In the example below, we create a string variable called txt, then we assign the text "Hello world!" to it. Then we write the value of the txt variable to the output: <?php $txt="hello world!"; echo $txt;?>.com
Concatenation String Operators There is only one string operator in PHP. The concatenation operator [. ] is used to join two string values together. <?php $txt1="hello world!"; $txt2="what a nice day!"; echo $txt1. " ". $txt2;?>.com
PHP strlen() function Sometimes it is useful to know the length of a string value. The strlen() function returns the length of a string, in characters. <?php echo strlen("hello world!");?>.com
PHP strpos() function The strpos() function is used to search for a character or a specific text within a string. If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE. <?php echo strpos("hello world!","world");?>.com
PHP Date() function <?php print( Today s Date is.date( l F d, y ));?>.com
PHP Arithmetic Operators.com
PHP Assignment Operators The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5..com
PHP Incr/Decr Operators.com
PHP Comparison Operators.com
PHP Logical Operators.com
PHP Array Operators.com
PHP If Statements SYNTAX: if (condition) code to be executed if condition is true; <?php $t=date("h"); if ($t<"20") echo "Have a good day!";?>.com
SYNTAX: PHP If Else Statements if (condition) code to be executed if condition is true; else code to be executed if condition is false;.com
PHP If Else Statements (cont ) <?php $t=date("h"); if ($t<"20") echo "Have a good day!"; else echo "Have a good night!";?>.com
SYNTAX: PHP If Else If Else Statements if (condition) code to be executed if condition is true; else if (condition) code to be executed if condition is true; else code to be executed if condition is false;.com
PHP If Else If Else Statements (cont ) <?php $t=date("h"); if ($t<"10") echo "Have a good morning!"; else if ($t<"20") echo "Have a good day!"; else echo "Have a good night!";?>.com
SYNTAX: PHP Switch Case Statements switch (n) case label1: code to be executed if n=label1; break; case label2: code to be executed if n=label2; break; default: code to be executed if n is different from both label1 and label2;.com
PHP Switch Case Statements <?php $favcolor="red"; switch ($favcolor) case "red": echo "Your favorite color is red! ; break; case "blue": echo "Your favorite color is blue! ; break; case "green": echo "Your favorite color is green! ; break; default: echo "Your favorite color is None of them!";?>.com
PHP Arrays An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: $cars1="volvo"; $cars2="bmw"; $cars3="toyota"; An array can hold many values under a single name, and you can access the values by referring to an index number..com
Creating a php Arrays PHP Arrays In PHP, the array() function is used to create an array: Types of php Arrays In PHP, there are three types of arrays: Indexed arrays - Arrays with numeric index Associative arrays - Arrays with named keys Multidimensional arrays - Arrays containing one or more arrays.com
PHP Indexed Arrays There are two ways to create indexed arrays: The index can be assigned automatically (index always starts at 0): $cars=array("volvo","bmw","toyota"); The index can be assigned manually: $cars[0]="volvo"; $cars[1]="bmw"; $cars[2]="toyota"; <?php $cars=array("volvo","bmw","toyota"); echo "I like ". $cars[0]. ", ". $cars[1]. " and ". $cars[2]. ".";?>.com
PHP Associative Arrays Associative arrays are arrays that use named keys that you assign to them. There are two ways to create an associative array: $age=array("peter"=>"35","ben"=>"37","joe"=>"43"); $age['peter']="35"; $age['ben']="37"; $age['joe']="43"; <?php $age=array("peter"=>"35","ben"=>"37","joe"=>"43"); echo "Peter is ". $age['peter']. " years old.";?>.com
What is MySQL? PHP Database Connectivity MySQL is a database system used on the web MySQL is a database system that runs on a server MySQL is ideal for both small and large applications MySQL is very fast, reliable, and easy to use MySQL supports standard SQL MySQL compiles on a number of platforms MySQL is free to download and use MySQL is developed, distributed, and supported by Oracle Corporation MySQL is named after co-founder Monty Widenius's daughter: My.com
Open a Connection to the MySQL Server Before we can access data in a database, we must open a connection to the MySQL server. In PHP, this is done with the mysqli_connect() function. Syntax PHP Database Connectivity mysqli_connect(host,username,password,dbname);.com
PHP Database Connectivity.com
PHP Database Connectivity Open a Connection to the MySQL Server <?php // Create connection $con=mysqli_connect( localhost, root, ", temp ); // Check connection if (mysqli_connect_errno($con)) echo "Failed to connect to MySQL: ". mysqli_connect_error();?>.com
PHP Database Connectivity Close a Connection to the MySQL Server <?php $con=mysqli_connect( localhost", root,, temp ); // Check connection if (mysqli_connect_errno($con)) echo "Failed to connect to MySQL: ". mysqli_connect_error(); mysqli_close($con);?>.com
PHP Database Connectivity Create a Database <?php $con=mysqli_connect( localhost, root, abc123 ); // Check connection if (mysqli_connect_errno()) echo "Failed to connect to MySQL: ". mysqli_connect_error(); // Create database $sql="create DATABASE temp"; if (mysqli_query($con,$sql)) echo "Database my_db created successfully"; else echo "Error creating database: ". mysqli_error();?>.com
Create a Table PHP Database <?php $con=mysqli_connect( localhost","peter","", temp"); // Check connection if (mysqli_connect_errno()) echo "Failed to connect to MySQL: ". mysqli_connect_error();.com
PHP Database // Create table $sql="create TABLE persons(firstname CHAR(30),Lastname CHAR(30),Age INT)"; // Execute query if (mysqli_query($con,$sql)) echo "Table persons created successfully"; else echo "Error creating table: ". mysqli_error();?>.com
Primary Keys and Auto Increment Fields PHP Database Each table in a database should have a primary key field. The following example sets the PID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field: $sql = "CREATE TABLE Persons ( PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID), Firstname CHAR(15), Lastname CHAR(15), Age INT(3) )";.com
Inserting Data into the Table PHP Database <?php $con=mysqli_connect( localhost", root,, temp"); // Check connection if (mysqli_connect_errno()) echo "Failed to connect to MySQL: ". mysqli_connect_error(); mysqli_query($con,"insert INTO Persons (FirstName, LastName, Age)VALUES ('Peter', 'Griffin',35)"); mysqli_query($con,"insert INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)"); mysqli_close($con);?>.com
Yet I will Not Consider this Presentation 100%. New Topic will be added at the end of the Slide after the Mid Sem Examination. By Mr. Amit Patel.com