PHP Functions

PHP Built-in Functions

PHP User Defined Functions

Besides the built-in PHP functions, it is possible to create your own functions.

PHP Function Arguments

PHP Default Argument Value

The following example shows how to use a default parameter. If we call the function setHeight() without arguments it takes the default value as argument:
			<?php 
			function setHeight(int $minheight = 50) {
  				echo "The height is : $minheight <br>";
			}

			setHeight(350);
			setHeight(); // will use the default value of 50
			setHeight(135);
			setHeight(80);
			?>
		

With int (typed parameter)

			setHeight(75.5);     // ✅ Becomes 75
			setHeight("100");    // ✅ Converts to 100
			setHeight("tall");   // ❌ Error in strict mode
		

If you want to add more parameters with defaults, just make sure required ones come first:

			function setBoxSize($width, $height = 100, $depth = 50) {
			    echo "Box: $width x $height x $depth <br>";
			}
			
			setBoxSize(200);
			setBoxSize(200,150);
			setBoxSize(200,150,20);
		

PHP Functions - Returning values

To let a function return a value, use the return statement:
			<?php
					function sum(int $x, int $y) {
  					$z = $x + $y;
  					return $z;
					}

					echo "5 + 10 = " . sum(5, 10) . "<br>";
					echo "7 + 13 = " . sum(7, 13) . "<br>";
					echo "2 + 4 = " . sum(2, 4);
			?>
		

Quadratic Formula Example:

			function solveQuadratic($a, $b, $c) {
			    $discriminant = ($b * $b) - (4 * $a * $c);
			
			    if ($discriminant < 0) {
			        return "No real solutions.";
			    } elseif ($discriminant == 0) {
			        $x = -$b / (2 * $a);
			        return "One real solution: x = $x";
			    } else {
			        $sqrtDisc = sqrt($discriminant);
			        $x1 = (-$b + $sqrtDisc) / (2 * $a);
			        $x2 = (-$b - $sqrtDisc) / (2 * $a);
			        return "Two real solutions: x₁ = $x1 and x₂ = $x2";
			    }
			}
			
			// Example usage:
			echo "Equation: x² + 5x + 6 = 0<br>";
			echo solveQuadratic(1, 5, 6) . "<br><br>";
			
			echo "Equation: x² + 4x + 4 = 0<br>";
			echo solveQuadratic(1, 4, 4) . "<br><br>";
			
			echo "Equation: x² + 2x + 5 = 0<br>";
			echo solveQuadratic(1, 2, 5);
		

Passing Arguments by Reference

			<?php
					function add_five(&$value) {
 						 $value += 5;
					}

					$num = 2;
					add_five($num);
					echo $num;
			?>
		

PHP Arrays

Create an Array in PHP

The count() Function

Loop Through an Indexed Array

PHP Associative Arrays

An associative array in PHP lets you use named keys (strings) instead of just numbers to access values.

Loop Through an Associative Array

PHP Contact List Webpage using Associative Arrays

	
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Contact List</title>
    <style>
	body {
	    font-family: Arial, sans-serif;
	    padding: 20px;
	    background: #f8f8f8;
	}
	h1 {
	    color: #333;
	}
	table {
	    border-collapse: collapse;
	    width: 80%;
	    background-color: #fff;
	}
	th, td {
	    border: 1px solid #ccc;
	    padding: 10px;
	    text-align: left;
	}
	th {
	    background-color: #eee;
	}
    </style>
</head>
<body>

<h1>📇 Contact List</h1>

<?php
// Our associative array of contacts
$contacts = [
    "alice" => [
	"name" => "Alice Smith",
	"email" => "alice@example.com",
	"city" => "Nashville"
    ],
    "bob" => [
	"name" => "Bob Johnson",
	"email" => "bob@example.com",
	"city" => "Memphis"
    ],
    "carol" => [
	"name" => "Carol White",
	"email" => "carol@example.com",
	"city" => "Knoxville"
    ]
];
?>

<table>
    <tr>
	<th>Name</th>
	<th>Email</th>
	<th>City</th>
    </tr>

    <?php foreach ($contacts as $id => $person) { ?>
	<tr>
	    <td><?php echo $person["name"]; ?></td>
	    <td><?php echo $person["email"]; ?></td>
	    <td><?php echo $person["city"]; ?></td>
	</tr>
    <?php } ?>
</table>

</body>
</html>
	

Reference