0% found this document useful (0 votes)
25 views67 pages

PHP

The document provides an overview of PHP, a server scripting language used for creating dynamic web pages. It covers PHP syntax, variables, data types, control structures, regular expressions, cookies, and MySQL database connectivity. Additionally, it includes code examples demonstrating various PHP functionalities.

Uploaded by

amudhanmanogaran
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)
25 views67 pages

PHP

The document provides an overview of PHP, a server scripting language used for creating dynamic web pages. It covers PHP syntax, variables, data types, control structures, regular expressions, cookies, and MySQL database connectivity. Additionally, it includes code examples demonstrating various PHP functionalities.

Uploaded by

amudhanmanogaran
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/ 67

PHP: Hypertext Preprocessor

• PHP is a server scripting language, and a


powerful tool for making dynamic and
interactive Web pages.
• PHP is a widely-used, free, and efficient
alternative to competitors such as Microsoft's
ASP.
• <?php
// PHP code goes here
?>
• <!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
PHP variables
• <?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
• A variable starts with the $ sign, followed by the
name of the variable
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive
($age and $AGE are two different variables)
• <?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with
multiple parameters.";
?>
• <?php
$txt1 = "Learn PHP";
$txt2 = "Welcome";
$x = 5;
$y = 4;

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


print "Study PHP " . $txt2 "<br>";
print $x + $y;
?>
Data types
• Integer
• Double
• String
• Boolean
<!DOCTYPE html>
<html>
<body>

<?php
echo strpos(“jsp php jstl php ","php");
?>

</body>
</html>
• <?php
$str = "Hello World!\n\n";
echo $str;
echo chop($str);
?>
• <?php
$str = "Hello World!";
echo $str . "<br>";
echo trim($str,"Hed!");
?>
• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
If..elseif..else
<!DOCTYPE html>
<html>
<body>

<?php
$t = date("H");
echo "<p>The hour (of the server) is " . $t;
echo ", and will give the following message:</p>";

if ($t < "10”) {


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

</body>
</html>
switch
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
while
<!DOCTYPE html>
<html>
<body>

<?php
$x = 0;

while($x <= 100) {


echo "The number is: $x <br>";
$x+=10;
}
?>

</body>
</html>
Do…while
<!DOCTYPE html>
<html>
<body>

<?php
$x = 1;

do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>

</body>
</html>
For loop
<!DOCTYPE html>
<html>
<body>

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

</body>
</html>
foreach
<!DOCTYPE html>
<html>
<body>

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>

</body>
</html>
Regular Expression

• ereg- it recognizes Portable Operating system


Interface extended regular expression(POSIX)
• preg- Perl compatible regular
expressions(PCRE)
Regular Expression

<!DOCTYPE html>
<html>
<body>

<?php
$str = "Visit College";
$pattern = "/college/i";
echo preg_match($pattern, $str);
?>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
$str = "Visit College";
$pattern = "/college/i";
echo preg_match($pattern, $str);
?>

</body>
</html>
<!DOCTYPE html>
<html>
<body>

<?php
$str = "Visit Microsoft!";
$pattern = "/microsoft/i";
echo preg_replace($pattern, “sun", $str);
?>

</body>
</html>
Regular expression example
Quantifier Matches
{n} Exactly n times.
{m,n} Between m and n times, inclusive.
{n,} n or more times.
+ One or more times (same as {1,}).
* Zero or more times (same as {0,}).
? Zero or one time (same as {0,1}).
Character class Description
• alnum Alphanumeric characters (i.e., letters
[a-zA-Z] or digits [0-9]).
• alpha Word characters (i.e., letters [a-zA-Z]).
• digit Digits.
• space White space.
• lower Lowercase letters.
• upper Uppercase letters.
• In PHP form data values are directly available as
implicit variables whose names match the names
of the corresponding form elements.
• This is know as implicit access
• Many web servers not allow this, because of
security problem
• Implicit arrays – these have keys that match the
form element names and values that were input
by the clients
• $_POST[],$_GET[]
• A cookie is often used to identify a user. A
cookie is a small file that the server embeds
on the user's computer. Each time the same
computer requests a page with a browser, it
will send the cookie too. With PHP, you can
both create and retrieve cookie values.
• A cookie is created with
the setcookie() function.
• $_COOKIE
• SESSION COOKIE
• PERSISTENT COOKIE
• <!DOCTYPE html>
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' is not set!";
} else {
echo "Cookie '" . $cookie_name . "' is set!<br>";
echo "Value is: " . $_COOKIE[$cookie_value];
}
?>
<p><strong>Note:</strong> You might have to reload the page to see the value of the cookie.</p>
</body>
</html>
Delete cookie
• <!DOCTYPE html>
<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
Enabled or not
• <!DOCTYPE html>
<?php
setcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled.";
} else {
echo "Cookies are disabled.";
}
?>
</body>
</html>
Mysql connect
• <?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);// print msg and exit from current php page
}

// 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();
?>
• <?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();
?>
• <?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();
?>

You might also like