0% found this document useful (0 votes)
79 views13 pages

PHP 5 Notes

This document provides an overview of PHP (Hypertext Preprocessor), which is a server-side scripting language used for web development. It discusses basic PHP syntax, how to declare variables and data types in PHP, arithmetic, assignment, comparison, and logical operators, if/else statements, switch statements, while and for loops. PHP allows embedding code into HTML files to create dynamic web pages and applications.
Copyright
© © All Rights Reserved
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)
79 views13 pages

PHP 5 Notes

This document provides an overview of PHP (Hypertext Preprocessor), which is a server-side scripting language used for web development. It discusses basic PHP syntax, how to declare variables and data types in PHP, arithmetic, assignment, comparison, and logical operators, if/else statements, switch statements, while and for loops. PHP allows embedding code into HTML files to create dynamic web pages and applications.
Copyright
© © All Rights Reserved
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/ 13

PHP Notes

PHP : Hypertext Preprocessor


PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.

<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>

Basic PHP Syntax


 A PHP script can be placed anywhere in the document.
 A PHP script starts with <?php and ends with ?>:
<?php
// PHP code goes here
?>

Creating (Declaring) PHP Variables


In PHP, a variable starts with the $ sign, followed by the name of the variable:

<!DOCTYPE html>
<html>
<body>
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
echo $txt;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>
</body>
</html>

Echo

<!DOCTYPE html>
<html>
<body>

<?php
$txt1 = "Learn PHP";
$txt2 = "W3Schools.com";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";


echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Teacher : S.Senthilnathan B.I.T (UCSC),
</body>Dip in Teach (Li & IT)
</html>
PHP Notes
PHP Data Types
Variables can store data of different types, and different data types can do different things.
PHP supports the following data types:
1. String :
$x = "Hello world!";
echo $x;
var_dump($x);
2. Integer
An integer data type is a non-decimal number between -2,147,483,648 and 2,147,483,647.

Rules for integers:


 An integer must have at least one digit
 An integer must not have a decimal point
 An integer can be either positive or negative
 Integers can be specified in three formats: decimal (10-based), hexadecimal (16-based -
prefixed with 0x) or octal (8-based - prefixed with 0)
<?php
$x = 5985;
var_dump($x);
?>
3. Float (floating point numbers - also called double)
A float (floating point number) is a number with a decimal point or a number in exponential form.
<?php
$x = 10.365;
var_dump($x);
?>
4. Boolean
A Boolean represents two possible states: TRUE or FALSE.
$x = true;
$y = false;
5. Array
An array stores multiple values in one single variable.
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>

PHP Arithmetic Operators


Operator Name Example
+ Addition $x + $y
- Subtraction $x - $y
* Multiplication $x * $y
/ Division $x / $y
% Modulus $x % $y
** Exponentiation $x ** $y

PHP Assignment Operators


Assignment Same as... Description
x=y x=y The left operand gets set to the value of the expression
on the right
x += y x=x+y Addition
x -= y x=x-y Subtraction
x *= y x=x*y Multiplication
x /= y x=x/y Division

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes
x %= y x=x%y Modulus

PHP Comparison Operators


Operator Name Example
== Equal $x == $y
=== Identical $x === $y
!= Not equal $x != $y
<> Not equal $x <> $y
!== Not identical $x !== $y
> Greater than $x > $y
< Less than $x < $y
>= Greater than or equal to
<= Less than or equal to $x <= $y

PHP Increment / Decrement Operators


Operator Name
++$x Pre-increment
$x++ Post-increment
--$x Pre-decrement
$x-- Post-decrement

PHP Logical Operators


Operator Name Example
and And $x and $y
or Or $x or $y
xor Xor $x xor $y
&& And $x && $y
|| Or $x || $y
! Not !$x

PHP String Operators


Operator Name Example Result
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

PHP - The if Statement


The if statement executes some code if one condition is true.

<?php <?php <?php


$t = date("H"); $t = date("H"); $t = date("H");

if ($t < "20") { if ($t < "20") { if ($t < "10") {


echo "Have a good day!"; echo "Have a good day!"; echo "Have a good morning!";
} } else { } elseif ($t < "20") {
?> echo "Have a good night!"; echo "Have a good day!";
} } else {
?> echo "Have a good night!";
}
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes
PHP Switch
<?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 neither red, blue, nor green!";
}
?>

The PHP while Loop


The while loop executes a block of code as long as the specified condition is true.

Syntax
while (condition is true) { <?php <?php
code to be executed; $x = 1; $x = 1;
} do {
while($x <= 5) { echo "The number is: $x <br>";
do { echo "The number is: $x <br>"; $x++;
code to be executed; $x++; } while ($x <= 5);
} } ?>
while (condition is true); ?>

The PHP for Loop


The for loop is used when you know in advance how many times the script should run.

Syntax
for (init counter; test counter; increment counter) {
code to be executed;
}

foreach ($array as $value) {


code to be executed;
}

<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>

<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes
}
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes
PHP Connect to MySQL
PHP 5 and later can work with a MySQL database using:
 MySQLi extension (the "i" stands for improved)
 PDO (PHP Data Objects)

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$conn->close();
?>

Create Database

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}

$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

CREATE TABLE

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to create table


$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP
)";

if ($conn->query($sql) === TRUE) {


echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . $conn->error;
}

$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

INSERT INTO TABLE

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO MyGuests (firstname, lastname, email)


VALUES ('John', 'Doe', '[email protected]')";

if ($conn->query($sql) === TRUE) {


echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

SELECT DATA FROM TABLE

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, firstname, lastname FROM MyGuests";


$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

UPDATE DATA

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";

if ($conn->query($sql) === TRUE) {


echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}

$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

DELETE DATA

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// sql to delete a record


$sql = "DELETE FROM MyGuests WHERE id=3";

if ($conn->query($sql) === TRUE) {


echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}

$conn->close();
?>

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

gapw;rp tpdhf;fs;
1. ,izak; MdJ xU xd;wpide;j tiyaikg;G MFk;. ,izgf;fq;fis tbtikg;gjw;F html, php Mfpa nkhopfs;
ngUk;ghYk; gad;gLj;jg;gLfpd;wd.
a. http Ntz;Liffspy; GET kw;Wk; POST Mfpa Ntz;Liffspw;fpilapyhd %d;W gpujhd NtWghLfis
vOJf.
b. xU ve;jpud;; fofj;jpdhy; mjd; Gjpa mq;fj;jtu;fspd; tpguq;fisg; ngWtjw;fhd xU tiyg; gf;fj;jpd; $W
fPNo jug;gLfpd;wJ

i. NkNy jug;gl;Ls;s tiyg;gf;fj;jpid cUthf;Ftjw;fhd html Fwpapid vOJf


ii. gpd;tUk; tbtikg;Gfis nra;tjw;fhd cs;sf css apid vOJf
1. Robotic Club vDk; jiyg;gpid ePy epukhf khw;Wjy;
2. gf;fj;jpd; gpd;Gyj;jpw;F xU gbkj;jpid Nru;j;jy;.
3. Competiton vDk; nrhy;ypid jbj;j> gr;irepw vOj;jhf khw;Wjy;
iii. jug;gl;l gf;fj;jpy; jpul;lg;gLk; juTfis “action.php” vDk; gf;fj;jpw;F mDg;Gtjw;fhd Fwpapid
vOJf
iv. ngw;gg; l;l juTfis juTj;jsj;py; Nrkpg;gjw;fhd Fwpapid vOJf?

2. fPNo jug;gl;Ls;s html gbtj;jpid tbtikg;gjw;fhd gad;gLj;jg;gl;l FwpaPlhdJ fPNo jug;gl;Ls;sJ mjidg;
G+uzg;gLj;Jf

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)


PHP Notes

<html>
……………….
…………………………………………………………….
……………….
<body>

……………Retrieve Data From Table</h2>

<…………………… action="" method="……………….." name=………………………..>


<………………….. style="width:300px;">
<…………….>Search by Book Code :</…………………>
<input type="………………" name="book_id" ………………….="Boook Code">
<br><br>
<input type="………………………." name="search" value="Search by Code">
</fieldset>
</form>

<…………………. style="width:300px;">
<………………………..>Result</……………………..>
<……………………. border="1px" width="100%" >
<…….><th>Book Title</th><th>Topic</th></……………>
<?php
$con = ………………………………….("localhost","root","");
$db=……………………………….. ($con,'lms');
if(…………………………………..)
{
$book_id= $_POST[………………………];
$query="……………………………………………………………………………………..;
$query_run=mysqli_query(……………………….., …………………………..);
while($row=…………………………………….. ($query_run))
{
?>
<tr>
<td><?php………………………………………………?></td>
<td><?php………………………………………………..?></td>
</tr>
<?php
}
}
?>
</table>
</fieldset>
</………………. >
</html>

Keywords:

mysqli_connect, legend, mysqli_select_db, echo $row['topic'], echo $row['book_name'],


isset($_POST['book_id']), 'book_id', $con, $query, mysqli_fetch_array, body

Teacher : S.Senthilnathan B.I.T (UCSC), Dip in Teach (Li & IT)

You might also like