PHP
PHP
<?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>";
</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;
</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");
</body>
</html>
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
}
$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();
?>