XAMPP: Cross – Apache, MySQL, Php, Perl + FileZilla, Tomcat NetBeans: IDE PHP Installation
PHP - XAMPP
PHP – NetBeans IDE
Netbeans- default browser
Netbeans – Starts a project
PHP – Starting a project
Netbeans – Setting a project
Basic PHP Syntax
Comment in Php
PHP Variables <?php $txt="Hello World!"; $x=16; echo $txt; ?> Hello World!
PHP Operators ArithmeticAssignmentComparisonLogicalString +== &&. (Append) -+=! =|| *-= ! /*=> %/=> = ++%=< - < =
if
if
switch
Numeric Arrays $cars=array("Saab","Volvo","BMW","Toyota"); or $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0]. " and ". $cars[1]. " are Swedish cars.";
Associative Arrays ID key => value $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); or $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is ". $ages['Peter']. " years old."; Peter is 32 years old.
Multidimensional Arrays $families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) ); Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) )
Multidimensional Arrays echo "Is ". $families['Griffin'][2]. " a part of the Griffin family?"; Is Megan a part of the Griffin family?
While Loops "; $i++; } ?> The number is 1 The number is 2 The number is 3 The number is 4 The number is 5
Do While Loops "; } while ($i The number is 2 The number is 3 The number is 4 The number is 5 The number is 6
For Loops "; } ?> The number is 1 The number is 2 The number is 3 The number is 4 The number is 5
Foreach Loops <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value. " "; } ?> one two three
Create a PHP Function My name is Kai Jim Refsnes
Create a PHP Function "; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> My name is Kai Jim Refsnes. My sister's name is Hege Refsnes. My brother's name is Stale Refsnes.
Create a PHP Function "; } echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); echo "My brother's name is "; writeName("Ståle","?"); ?> My name is Kai Jim Refsnes. My sister's name is Hege Refsnes! My brother's name is Ståle Refsnes?i
Create a PHP Function = 17
$_GET variable Name: Age: Welcome. You are years old! When using method="get" in HTML forms, all variable names and values are displayed in the URL.
$_POST variable Name: Age: Welcome ! You are years old. Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
$_REQUEST Variable Welcome ! You are years old The predefined $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The $_REQUEST variable can be used to collect form data sent with both the GET and POST methods.
PHP Date() Function date(format [, timestamp]) "; echo date("Y-m-d"); ?> 2011/11/
PHP Date() Function mktime(hour,minute,second,month,day,year,is_dst) Tomorrow is 2011/11/18
PHP include() Function Welcome to my home page! Some text.
PHP include() Function menu.php Home Tutorials References Examples About Us Contact Us Welcome to my home page. Some text.
PHP File Handling Reading a File Line by Line "; } fclose($file); ?>
PHP File Handling Reading a File Character by Character
PHP File Upload Create an Upload-File Form Filename:
Create The Upload Script upload_file.php 0) { echo "Error: ". $_FILES["file"]["error"]. " "; } else { echo "Upload: ". $_FILES["file"]["name"]. " "; echo "Type: ". $_FILES["file"]["type"]. " "; echo "Size: ". ($_FILES["file"]["size"] / 1024). " Kb "; echo "Stored in: ". $_FILES["file"]["tmp_name"]; } ?>
Save uploaded File upload_file.php
Create a Cookie setcookie(name, value, expire, path, domain); 60 sec * 60 min * 24 hours * 30 days
Retrive a Cookie "; else echo "Welcome guest! "; ?>
Delete a Cookie
PHP Session Variables Starting a PHP Session Before you can store user information in your PHP session, you must first start up the session. Note: The session_start() function must apear BEFORE the tag:
PHP Session Variables
PHP Session Variables
PHP Destroying a Session Free a session variable
PHP mail() Function
PHP mail() Function Subject: Message: "; } ?>
Error Handling
Error Handling Error: [$errno] $errstr "; echo "Ending Script"; die(); } //set error handler set_error_handler("customError",E_USER_WARNING); //trigger error $test=2; if ($test>1) { trigger_error("Value must be 1 or below",E_USER_WARNING); } ?>
Error Handling 1) { throw new Exception("Value must be 1 or below"); } return true; } //trigger exception in a "try" block try { checkNum(2); //If the exception is thrown, this text will not be shown echo 'If you see this, the number is 1 or below'; } //catch exception catch(Exception $e) { echo 'Message: '.$e->getMessage(); } ?> Message: Value must be 1 or below
Filter & Sanitize Check if the "url" input of the "POST" type exists If the input variable exists, sanitize (take away invalid characters) and store it in the $url variable "
MySQL Workbench
Connect / Close a DB mysql_connect(servername,username,password);
Create a Database if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: ". mysql_error(); }
Create a Table // Create table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( FirstName varchar(15), LastName varchar(15), Age int )"; // Execute query mysql_query($sql,$con);
Create a Table mysql_select_db("my_db", $con); $sql = "CREATE TABLE Persons ( personID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(personID), FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con);
Insert a Table mysql_select_db("my_db", $con); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')");
Insert Data From a Form Into a Database Firstname: Lastname: Age:
Insert Data From a Form Into a Database mysql_select_db("my_db", $con); $sql="INSERT INTO Persons (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: '. mysql_error()); } echo "1 record added";
Select Data From a Database Table "; } mysql_close($con); ?>
Display the Result in an HTML Table Firstname Lastname "; while($row = mysql_fetch_array($result)) { echo " "; echo " ". $row['FirstName']. " "; echo " ". $row['LastName']. " "; echo " "; } echo " "; mysql_close($con); ?>
The WHERE clause "; } ?>
Update Data In a Database
Delete Data In a Database