PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo("<p style= 'color: red'>hello world</p>");
?>
</body>
</html>
<?php
echo("hello world");
?>

In PHP, the echo statement is used to output one or more strings or variables to the browser or standard output. It’s commonly used to display content such as HTML, text, or the values of variables within a web page.

Here’s how the echo statement works:

  1. Outputting Text: You can use echo to output text directly:

    1
    2
    3
    <?php
    echo "Hello, world!";
    ?>

    Output: Hello, world!

  2. Outputting Variables: You can output the value of variables using echo:

    1
    2
    3
    4
    <?php
    $name = "John";
    echo "Hello, $name!";
    ?>

    Output: Hello, John!

  3. Outputting HTML: You can use echo to output HTML code:

    1
    2
    3
    4
    5
    6
    7
    8
    <?php
    echo "<h1>Welcome to my website</h1>";
    ?>

    <?php
    echo("<p style= 'color: red'>hello world</p>");
    ?>

    Output: <h1>Welcome to my website</h1> (renders as a heading in the browser)

  4. Multiple Outputs: You can use echo to output multiple strings or variables separated by commas:

    1
    2
    3
    4
    5
    <?php
    $name = "John";
    $age = 30;
    echo "Name: ", $name, ", Age: ", $age;
    ?>

    Output: Name: John, Age: 30

  5. Echo vs. Print: echo is a language construct in PHP, whereas print is a function. echo has a slightly faster execution time and can take multiple parameters separated by commas, whereas print only takes one parameter and always returns 1, so it’s typically used for simple output.

dollar sign

$variable = …..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$name = "John";
$age = 28;
echo("<p style= 'color: red'>hello world</p>");
echo("<p style= 'color: blue'>this is $name who is $age years old</p>");
?>
</body>
</html>

strtolowercase

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$phrase = "to be or not to be HAHAHHAHAHAHA";
$age = 28;
$gap = 1.111111;
$ismale = false;
echo(strtolower($phrase)."<br>");
echo(strtoupper($phrase));
?>
</body>
</html>

to be or not to be hahahhahahaha
TO BE OR NOT TO BE HAHAHHAHAHAHA

index

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$phrase = "to be or not to be HAHAHHAHAHAHA";
$age = 28;
$gap = 1.111111;
$ismale = false;
echo(strtolower($phrase)."<br>");
echo(strtoupper($phrase)."<br>");
echo $phrase[1];
?>
</body>
</html>

to be or not to be hahahhahahaha
TO BE OR NOT TO BE HAHAHHAHAHAHA
o

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$phrase = "to be or not to be HAHAHHAHAHAHA";
$age = 28;
$gap = 1.111111;
$ismale = false;
echo(strtolower($phrase)."<br>");
echo(strtoupper($phrase)."<br>");
echo $phrase[1];
echo "<br>";
$phrase[0] = "d";
echo($phrase);
?>
</body>
</html>

to be or not to be hahahhahahaha
TO BE OR NOT TO BE HAHAHHAHAHAHA
o
do be or not to be HAHAHHAHAHAHA

replacement

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$phrase = "to be or not to be HAHAHHAHAHAHA";
echo(str_replace("not", "yes", $phrase) ) ;
?>
</body>
</html>

substring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$phrase = "to be or not to be HAHAHHAHAHAHA";
echo substr($phrase, 3,2);
?>
</body>
</html>

be

Math

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
echo 5 + 12;
?>
</body>
</html>

17

form action

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php ?>
<form action = "site.php" method="get">
Name: <input type="text" name = "name">
<input type="submit" >
</form>
</body>
</html>

image-20240429115736036

get input

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php ?>
<form action = "site.php" method="get">
Name: <input type="text" name = "name">
<input type="submit" >
</form>
<br>
your name is: <?php echo $_GET["name"] ?>
</body>
</html>

caculator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<form action = "site.php" method="get">
num1: <input type="number" name = "num1">
<br>
num2: <input type="number" name = "num2">
<input type="submit">
</form>

answer <?php echo $_GET["num1"] + $_GET["num2"] ?>
</body>
</html>

mad libs game

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<form action = "site.php" method="get">
color: <input type="text" name = "color">
<br>
plural noun: <input type="text" name = "pluralNoun">
<br>
celebrity: <input type="text" name = "celebrity">
<input type="submit">
</form>

<?php
$color = $_GET["color"];
$pluralNoun = $_GET["pluralNoun"];
$celebrity = $_GET["celebrity"];
echo "rose are $color <br>";
echo "$pluralNoun are blue<br>";
echo "I love $celebrity <br>";

?>
</body>
</html>

http://localhost:4000/site.php?color=redf&pluralNoun=flowers&celebrity=haha

URL parameters in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<form action = "site.php" method="get">
color: <input type="text" name = "color">
<br>
plural noun: <input type="text" name = "pluralNoun">
<br>
celebrity: <input type="text" name = "celebrity">
<input type="submit">
</form>

<?php
$color = $_GET["color"];
$pluralNoun = $_GET["pluralNoun"];
$celebrity = $_GET["age"];
echo "rose are $color <br>";
echo "$pluralNoun are blue<br>";
echo "I love $celebrity <br>";

?>
</body>
</html>

image-20240430115723243

POST & GET

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<form action = "site.php" method="get">
color: <input type="text" name = "password">
<input type="submit">
<br>

</form>

<?php
$color = $_GET["password"];
?>
</body>
</html>
// you cant show password in the URL!!! now it's time to use POST



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<form action = "site.php" method="post">
color: <input type="text" name = "password">
<input type="submit">
<br>

</form>

<?php
echo $_POST["password"];
?>
</body>
</html>

  • when you use post, it’s more secure, inf wont be showed up in the URL

  • when you use post, you can potentially get more information

  1. $_POST:
    • Use $_POST when you’re dealing with sensitive or large data, such as passwords or file uploads, as it sends data in the HTTP request body, making it more secure and allowing larger data sizes compared to $_GET.
    • Use it for actions that modify data or have side effects, like submitting a form to add or update information in a database.
  2. $_GET:
    • Use $_GET when you want to pass data via URL parameters. It’s commonly used for non-sensitive data, like search queries or page filters, as the data is visible in the URL.
    • It’s useful for bookmarking or sharing URLs, as the parameters are part of the URL.
    • However, avoid using $_GET for sensitive data like passwords, as it exposes the data in the URL, making it visible to users and potentially insecure.

Arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>

<?php
$friends = array("Kevin","Karen","Dan","Jim");
echo $friends[0];
?>
</body>
</html>

result: Kevin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html>
<body>

<?php
// function example:
function myFunction() {
echo "This text comes from a function";
}

// create array:
$myArr = array("Volvo", 15, ["apples", "bananas"], myFunction);

// calling the function from the array item:
echo $myArr[2][0];
?>

</body>
</html>

apples

1
2
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);

checkbox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action = "site.php" method="post">
Apple<input type="checkbox" name = "fruits[]" value="apple"><br>
Cheery<input type="checkbox" name = "fruits[]" value="cherry"><br>
Pear<input type="checkbox" name = "fruits[]" value="pear"><br>
Grape<input type="checkbox" name = "fruits[]" value="grape"><br>

<input type="submit" >
</form>
<br>

<?php
$fruits = $_POST["fruits"]; //it's an array
echo $fruits[0];
?>
</body>
</html>
// it gonna print out the first checkbox you check

Associative Array

key value pair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action = "site.php" method="post">
<input type="text" name="student">
<input type="submit">
</form>
<br>

<?php
$grade = ["Jim"=>"A+", "Pam"=>"B-", "Oscar"=>"C+"]; //it's an array
echo $grade[$_POST["student"]];
?>
</body>
</html>

result is A+

1
2
3
4
5
$myCar = [];
$myCar["brand"] = "Ford";
$myCar["model"] = "Mustang";
$myCar["year"] = 1964;
var_dump($myCar);

You can have arrays with both indexed and named keys:

1
2
3
4
$myArr = [];
$myArr[0] = "apples";
$myArr[1] = "bananas";
$myArr["fruit"] = "cherries";

Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action = "site.php" method="post">
<input type="text" name="name">
<input type="submit">
</form>
<br>

<?php
function sayHi($name){
echo "Hello $name <br>";
}
sayHi($_POST["name"]);
sayHi("David");
sayHi("wow");

?>
</body>
</html>

Execute a function item:

1
2
3
4
5
6
7
function myFunction() {
echo "I come from a function!";
}

$myArr = array("Volvo", 15, myFunction);

$myArr[2]();

return in PHP function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action = "site.php" method="post">

</form>
<br>

<?php
function cube($num){
return $num * $num * $num;
}
$res = cube(4);
echo $res;

?>
</body>
</html>

if

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action = "site.php" method="post">

</form>
<br>

<?php
$isMale = true;
if($isMale){
echo "male";
}

?>
</body>
</html>

switch

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
switch (expression) {
case label1:
//code block
break;
case label2:
//code block;
break;
case label3:
//code block
break;
default:
//code block
}





$favcolor = "red";

switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
"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!";
}

PHP Loops

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//while
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}

//break
$i = 1;
while ($i < 6) {
if ($i == 3) break;
echo $i;
$i++;
}

//continue
$i = 0;
while ($i < 6) {
$i++;
if ($i == 3) continue;
echo $i;
}

//The while loop syntax can also be written with the endwhile statement like this
$i = 1;
while ($i < 6):
echo $i;
$i++;
endwhile;

//do while
$i = 1;

do {
echo $i;
$i++;
} while ($i < 6);

//For Loop
for ($x = 0; $x <= 10; $x++) {
if ($x == 3) break;
echo "The number is: $x <br>";
}

//for each (associate array)
$members = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach ($members as $x => $y) {
echo "$x : $y <br>";
}


// for each (alternative syntax)
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $x) :
echo "$x <br>";
endforeach;

Byref

When looping through the array items, any changes done to the array item will, by default, NOT affect the original array

BUT, by using the & character in the foreach declaration, the array item is assigned by reference, which results in any changes done to the array item will also be done to the original array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!DOCTYPE html>
<html>
<body>

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

foreach ($colors as $x) {
if ($x == "blue") $x = "pink";
}

var_dump($colors);
?>
</pre>

</body>
</html>


res:

array(4) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
[3]=>
string(6) "yellow"
}

//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as &$x) { //!!! difference is here (=
if ($x == "blue") $x = "pink";
}

var_dump($colors);


res:
array(4) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "pink"
[3]=>
string(6) "yellow"
}

PHP Update Array Items

To update an existing array item, you can refer to the index number for indexed arrays, and the key name for associative arrays.

Change the second array item from “BMW” to “Ford”:

1
2
$cars = array("Volvo", "BMW", "Toyota");
$cars[1] = "Ford";

There are different techniques to use when changing item values in a foreach loop.

One way is to insert the & character in the assignment to assign the item value by reference, and thereby making sure that any changes done with the array item inside the loop will be done to the original array:

Change ALL item values to “Ford”:

1
2
3
4
5
6
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as &$x) {
$x = "Ford";
}
unset($x);
var_dump($cars);

Note: Remember to add the unset() function after the loop.

Without the unset($x) function, the $x variable will remain as a reference to the last array item.

To demonstrate this, see what happens when we change the value of $x after the foreach loop:

1
2
3
4
5
6
7
8
$cars = array("Volvo", "BMW", "Toyota");
foreach ($cars as &$x) {
$x = "Ford";
}

$x = "ice cream";

var_dump($cars);

array(3) {
[0]=>
string(4) “Ford”
[1]=>
string(4) “Ford”
[2]=>
&string(9) “ice cream”
}

unset

The unset() function in PHP is used to destroy variables. It removes the variable from memory, making it no longer accessible or usable. For example:

1
2
code$x = 10;
unset($x);

After executing unset($x), the variable $x will no longer exist in the script, and attempting to access it will result in an error.

add array

To add items to an existing array, you can use the bracket [] syntax.

Add one more item to the fruits array:

1
2
$fruits = array("Apple", "Banana", "Cherry");
$fruits[] = "Orange";

To add multiple items to an existing array, use the array_push() function.

Add three item to the fruits array:

1
2
$fruits = array("Apple", "Banana", "Cherry");
array_push($fruits, "Orange", "Kiwi", "Lemon");

To add multiple items to an existing array, you can use the += operator.

Add two items to the cars array:

1
2
$cars = array("brand" => "Ford", "model" => "Mustang");
$cars += ["color" => "red", "year" => 1964];

remove array

To remove an existing item from an array, you can use the array_splice() function.

With the array_splice() function you specify the index (where to start) and how many items you want to delete.

Remove the second item: (it will rearrange the index automatically)

1
2
$cars = array("Volvo", "BMW", "Toyota");
array_splice($cars, 1, 1);

array(2) {
[0]=>
string(5) “Volvo”
[1]=>
string(6) “Toyota”
}

You can also use the unset() function to delete existing array items.

Note: The unset() function does not re-arrange the indexes, meaning that after deletion the array will no longer contain the missing indexes.

Remove the second item:

1
2
$cars = array("Volvo", "BMW", "Toyota");
unset($cars[1]);
1
2
3
4
5
6
array(2) {
[0]=>
string(5) "Volvo"
[2]=>
string(6) "Toyota"
}

You can also use the array_diff() function to remove items from an associative array.

This function returns a new array, without the specified items.

Remove Item From an Associative Array

To remove items from an associative array, you can use the unset() function.

Specify the key of the item you want to delete.

Remove the “model”:

1
2
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
unset($cars["model"]);

Using the array_diff Function

You can also use the array_diff() function to remove items from an associative array.

This function returns a new array, without the specified items.

Create a new array, without “Mustang” and “1964”:

1
2
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
$newarray = array_diff($cars, ["Mustang", 1964]);

Note: The array_diff() function takes values as parameters, and not keys.