0% found this document useful (0 votes)
70 views

PHP Tutorial

This PHP tutorial document provides an introduction to PHP including PHP syntax, variables, operators, conditional statements, arrays, looping, functions, forms, date functions, includes, working with databases including inserting, selecting, updating and deleting data. It covers basic to more advanced PHP concepts and techniques.

Uploaded by

emehdy_gmail_com
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

PHP Tutorial

This PHP tutorial document provides an introduction to PHP including PHP syntax, variables, operators, conditional statements, arrays, looping, functions, forms, date functions, includes, working with databases including inserting, selecting, updating and deleting data. It covers basic to more advanced PHP concepts and techniques.

Uploaded by

emehdy_gmail_com
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

PHP TUTORIAL

PHP TUTORIAL

PHP Syntax

<?php

?>

Hello World

Comments in PHP

<html> <body> <?php echo "Hello World"; ?> </body> </html>

<html> <body> <?php //This is a comment /* This is a comment block */ ?> </body> </html>

PHP TUTORIAL

PHP Variables $var_name = value;

<?php $txt="Hello World!"; $x=16; ?>

Hello World

Concatenation Operator

<?php $txt="Hello World"; echo $txt; ?>

<?php $txt1="Hello World!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?>

PHP TUTORIAL

PHP Variables $var_name = value;

<?php $txt="Hello World!"; $x=16; ?>

Hello World

Concatenation Operator , strlen , strpos

<?php $txt="Hello World"; echo $txt; ?>

<?php $txt1="Hello World!"; $txt2="What a nice day!"; echo $txt1 . " " . $txt2; ?> <?php echo strlen("Hello world!"); ?> <?php echo strpos("Hello world!","world"); ?>

PHP TUTORIAL

PHP Operators

PHP TUTORIAL

PHP Operators

PHP TUTORIAL

PHP Operators

PHP TUTORIAL

Conditional Statements .. The if Statement

<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; ?> </body> </html>
The if...else Statement

<html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement

<html> <body> <?php switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Arrays

$cars1="Saab"; $cars2="Volvo"; $cars3="BMW";

$cars=array("Saab","Volvo","BMW","Toyota");

$cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota";

<?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars."; ?>

PHP TUTORIAL

Associative Arrays PHP Switch Statement

$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);

$ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34";

<?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?>

PHP TUTORIAL

Multidimensional Arrays PHP Switch Statement

$families = array ( "Griffin"=> array ( "Peter, "Lois, "Megan )

,
"Quagmire"=> array ( "Glenn ) , "Brown"=> array ( "Cleveland, "Loretta, "Junior ) );

PHP TUTORIAL

PHP Switch Statement Looping

<html> <body> <?php $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Looping

<html> <body> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Looping

<html> <body> <?php for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "<br />"; } ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Looping

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

PHP TUTORIAL

PHP Switch Statement Functions

<html> <body> <?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement parameters Functions passing

<html> <body>

<?php function writeName($fname) { echo $fname . " Refsnes.<br />"; }


echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Functions - two parameters

<html> <body>

<?php function writeName($fname,$punctuation) { echo $fname . " Refsnes" . $punctuation . "<br />"; }
echo "My name is "; writeName("Kai Jim","."); echo "My sister's name is "; writeName("Hege","!"); echo "My brother's name is "; writeName("Stle","?"); ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement values Functions Return

<html> <body>

<?php function add($x,$y) { $total=$x+$y; return $total; }


echo "1 + 16 = " . add(1,16); ?> </body> </html>

PHP TUTORIAL

PHP Switch Statement Form Handling

<html> <body>

<form action="welcome.php" method="post"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>
</body> </html>

<html> <body> Welcome <?php echo $_POST["fname"]; ?>!<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html>

PHP TUTORIAL

PHP Switch Statement $_GET Variable Form Handling -

<form action="welcome.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>

welcome.php?fname=Peter&age=37

Welcome <?php echo $_GET["fname"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!

PHP TUTORIAL

PHP Switch Statement $_GET Variable Form Handling -

<form action="welcome.php" method="get"> Name: <input type="text" name="fname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>

welcome.php?fname=Peter&age=37

Welcome <?php echo $_GET["fname"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old!

PHP TUTORIAL

PHP Switch Statement Date()

<?php echo date("Y/m/d") . "<br />"; echo date("Y.m.d") . "<br />"; echo date("Y-m-d"); ?>

<?php $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "Tomorrow is ".date("Y/m/d", $tomorrow); ?>

PHP TUTORIAL

Server Side Statement PHP Switch Includes (SSI)

<html> <body>

<?php include("header.php"); ?> <h1>Welcome to my home page!</h1> <p>Some text.</p>


</body> </html>

<html> <body> <div class="leftmenu"> <?php include("menu.php"); ?> </div> <h1>Welcome to my home page.</h1> <p>Some text.</p> </body> </html>

PHP TUTORIAL

Server Side Statement PHP Switch Includes (SSI) - require()

<html> <body>

<?php include("wrongFile.php"); echo "Hello World!"; ?>


</body> </html> <html> <body> <?php require("wrongFile.php"); echo "Hello World!"; ?> </body> </html>

MYSQL

PHP TUTORIAL

Database Tables PHP Switch Statement

PHP TUTORIAL

mysql_query PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } 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')"); mysql_close($con); ?>

PHP TUTORIAL

Insert Data Statement PHP Switch From a Form Into a Database

<html> <body>

<form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>
</body> </html>

PHP TUTORIAL

Insert Data Statement PHP Switch From a Form Into a Database

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } 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"; mysql_close($con) ?>

PHP TUTORIAL

fetch_array PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }


mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } mysql_close($con); ?>

PHP TUTORIAL

HTML Result PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons"); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> </tr>"; while($row = mysql_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['FirstName'] . "</td>"; echo "<td>" . $row['LastName'] . "</td>"; echo "</tr>"; } echo "</table>"; mysql_close($con); ?>

PHP TUTORIAL

Where PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter'"); while($row = mysql_fetch_array($result)) { echo $row['FirstName'] . " " . $row['LastName']; echo "<br />"; } ?>

PHP TUTORIAL

Order by PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $result = mysql_query("SELECT * FROM Persons ORDER BY age"); while($row = mysql_fetch_array($result)) { echo $row['FirstName']; echo " " . $row['LastName']; echo " " . $row['Age']; echo "<br />"; } mysql_close($con); ?>

PHP TUTORIAL

Update PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }


mysql_select_db("my_db", $con); mysql_query("UPDATE Persons SET Age = '36' WHERE FirstName = 'Peter' AND LastName = 'Griffin'"); mysql_close($con); ?>

PHP TUTORIAL

Delete PHP Switch Statement

<?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); }


mysql_select_db("my_db", $con); mysql_query("DELETE FROM Persons WHERE LastName='Griffin'"); mysql_close($con); ?>

You might also like