Professional Documents
Culture Documents
Assignment No - 3
Assignment No - 3
A variable can have a short name (like x and y) or a more descriptive name
Example; (age, carname, total_volume).
Example:
<?php
echo strlen("Hello PHP");
?>
Output: 9
Example:
<?php
echo strpos("Hello world!", "world");
?>
Output: 6
The first character position in a string is 0 (not 1).
6. PHP Operators
Operators are used to perform operations on variables and values.
Arithmetic operators
+ Addition * Multiplication
<?php <?php
$x = 10; $x = 10;
$y = 6; $y = 6;
echo $x + $y; echo $x * $y;
?> ?>
Output: 16 Output: 60
- Subtraction / Division
<?php <?php
$x = 10; $x = 12;
$y = 6; $y = 6;
echo $x - $y; echo $x / $y;
?> ?>
Output: 4 Output: 2
Comparison operators
== Equal
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
?>
=== Identical
<?php
$x = 100;
$y = "100";
var_dump($x === $y); // returns false because types are not equal
?>
!= Not equal
<?php
$x = 100;
$y = "100";
var_dump($x != $y); // returns false because values are equal
?>
Increment/Decrement operators
++$x Pre-increment (Increments $x by one, then returns $x)
<?php
$x = 10;
echo ++$x;
?>
Output: 11
Logical operators
&& And (True if both $x and $y are true)
<?php
$x = 100;
$y = 50;
7. Conditional Statements
if statement - executes some code if one condition is true
<?php
$x = 100;
$y = 50;
if...else statement - executes some code if a condition is true and another code if that
condition is false
<?php
$t = date("H");
if ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
if...elseif....else statement - executes different codes for more than two conditions
<?php
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
<?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!";
}
?>
Output: Your favorite color is red!
8. What is an Array?
An array is a special variable, which can hold more than one value at a time. An array can
hold many values under a single name, and you can access the values by referring to an index
number.
<?php
$phones = array("Vivo", "Oppo", "Realme");
echo "I like " . $phones[0] . ", " . $phones[1] . " and " .
$phones[2] . ".";
?>
<?php
$phones = array("Vivo", "Oppo", "Realme");
echo "I like " . $phones[0] . ", " . $phones[1] . " and " .
$phones[2] . ".";
?>
Output: I like Vivo, Oppo and Realme.
Associative arrays - Associative arrays are arrays that use named keys that you assign to
them.
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Output: Peter is 35 years old.
<?php
echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
?>
Output:
Volvo: In stock: 22, sold: 18.
BMW: In stock: 15, sold: 13.
Saab: In stock: 5, sold: 2.
Land Rover: In stock: 17, sold: 15.
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Document Title: Course Template
Document Code: Rev. No.: Effective Date: Page7 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true.
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>
Output:
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
foreach - loop works only on arrays, and is used to loop through each key/value pair in an
array.
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
Output:
red
green
blue
yellow
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// collect value of input field
$name = $_REQUEST['fname'];
if (empty($name)) {
echo "Name is empty";
} else {
echo $name;
}
Document Title: Course Template
Document Code: Rev. No.: Effective Date: Page10 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183
} ?>
</body>
</html>
15. Create a Connection to a MySQL Database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$database = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>