You are on page 1of 11

Republic of the Philippines

CAMIGUIN POLYTECHNIC STATE COLLEGE


Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

TE4 – DYNAMIC WEB APPLICATION


M-9:00AM-10:00AM, 1:00PM-4:00PM
TH-4:00PM – 5:00PM

Instructor: Ken Labadan


Email: klabadan@cpsc.edu.ph

Student ID No. 17-0094 Date June 14, 2021


Student Name Eleazar P. Chavez Jr.

Note: Each of the number must have an example.


1. Naming Rules for Variables
 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)

A variable can have a short name (like x and y) or a more descriptive name
Example; (age, carname, total_volume).

2. String Variables in PHP


The string variables string is a collection of characters it can contain alphanumeric characters
and underscores. 
Example:
<?php
$txt = "CPSC!";
echo "I love $txt";
?>

Output: I love CPSC!

3. The Concatenation Operator


The concatenation operator (.) is used to combine two string values to create one string.
Example:
<?php
$txt1 = "Hello";
$txt2 = " CPSC!";
echo $txt1 . $txt2;
?>  

Output: Hello CPSC!

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page1 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

4. The strlen() function


The strlen() function returns the length of a string.

Example:
<?php
echo strlen("Hello PHP");
?>

Output: 9

5. The strpos() function


The PHP strpos() function searches for a specific text within a string. If a match is found,
the function returns the character position of the first match. If no match is found, it will return
FALSE.

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

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page2 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

 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
?>  

!== Not identical


<?php
$x = 100;  
$y = "100";
var_dump($x !== $y);  // returns true because types are not equal
?>  

> Greater than


<?php
$x = 100;
$y = 50;
var_dump($x > $y);  // returns true because $x is greater than $y
?>  

>= Greater than or equal to


<?php
$x = 50;
$y = 50;
var_dump($x >= $y); // returns true because $x is greater than or equal to $y
?>  

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page3 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

 Increment/Decrement operators
++$x Pre-increment (Increments $x by one, then returns $x)
<?php
$x = 10;  
echo ++$x;
?>  
Output: 11

$x++ Post-increment (Returns $x, then increments $x by one)


<?php
$x = 10;  
echo $x++;
?>  
Output: 10

--$x Pre-decrement (Decrements $x by one, then returns $x)


<?php
$x = 10;  
echo --$x;
?>  
Output: 9

$x-- Post-decrement (Returns $x, then decrements $x by one)


<?php
$x = 10;  
echo $x--;
?>  
Output: 10

 Logical operators
&& And (True if both $x and $y are true)
<?php
$x = 100;  
$y = 50;

if ($x == 100 && $y == 50) {


    echo "Hello world!";
}
?>  
Output: Hello world! (because both $x and $y are true)

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page4 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

|| Or (True if either $x or $y is true)


<?php
$x = 100;  
$y = 50;
if ($x == 100 || $y == 80) {
    echo "Hello world!";
}
?>  
Output: Hello world! (because $x is true)

xor Xor (True if either $x or $y is true, but not both)


<?php
$x = 100;  
$y = 50;
if ($x == 100 xor $y == 80) {
    echo "Hello world!";
}
?>  
Output: Hello world! (because only $x is true)

7. Conditional Statements
 if statement - executes some code if one condition is true
<?php
$x = 100;  
$y = 50;

if ($x == 100 && $y == 50) {


    echo "Hello world!";
}
?>  
Output: Hello world!

 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!";

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page5 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

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

 switch statement - The switch statement is used to perform different actions based on


different conditions.

<?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] . ".";
?>

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page6 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

Output: I like Vivo, Oppo and Realme.

9. PHP kinds of arrays


 Indexed arrays - Arrays with a numeric index. The index can be assigned automatically and
manually (index always starts at 0)

<?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.

 Multidimensional arrays - A multidimensional array is an array containing one or more arrays.

<?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.

10. PHP Looping


 while - loops through a block of code as long as the specified condition is true.

<?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

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

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

 for - loops through a block of code a specified number of times.


<?php
for ($x = 0; $x <= 5; $x++) {
    echo "The number is: $x <br>";
}
?>
Output:
The number is: 0
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>";

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page8 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

}
?>

Output:
red
green
blue
yellow

11. PHP Functions


A function is a block of statements that can be used repeatedly in a program. A function will be
executed by a call to the function.
<?php
function writeMsg() {
    echo "Hello world!";
}
writeMsg(); // call the function
?>
Output: Hello world!

12. PHP $_GET Variable


PHP $_GET is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="get". $_GET can also collect data sent in the URL.
$_GET is an array of variables passed to the current script via the URL parameters.

<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>

"welcome_get.php" looks like this:


<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page9 of 11
Republic of the Philippines
CAMIGUIN POLYTECHNIC STATE COLLEGE
Balbagon 9100, Mambajao, Camiguin
Tel(088)8890183

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

13. PHP $_POST Function


PHP $_POST is a PHP super global variable which is used to collect form data after
submitting an HTML form with method="post". $_POST is also widely used to pass variables.
$_POST is an array of variables passed to the current script via the HTTP POST method.

<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>

"welcome.php" looks like this:


<html>
<body>
Welcome <?php echo $_POST["name"]; ?><br>
Your email address is: <?php echo $_POST["email"]; ?>
</body>
</html>

14. The PHP $_REQUEST Variable


PHP $_REQUEST is a PHP super global variable which is used to collect data after
submitting an HTML form.

<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

INSTITUTE OF ENGINEERING AND COMPUTER STUDIES

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

Document Title: Course Template


Document Code: Rev. No.: Effective Date: Page11 of 11

You might also like