Category Archives: PHP

MySQLi OOP connection example PHP

$mysqli = new mysqli("localhost", "my_user", "my_password", "world"); if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); } $query = "SELECT * FROM users WHERE user_id=’$userid’"; if ($result = $mysqli->query($query)) { while ($row = $result->fetch_assoc()) { echo $row['name'] ."<br />"; } $result->close(); … Continue reading

Posted in PHP | Leave a comment

Recursive Functions in PHP (Simple Examples)

A recursive function is a regular function which calls itself.. // Factorial example (ie 5 * 4 * 3 * 2 * 1) function factorial($n) { if ($n == 1) return 1; return $n * factorial($n-1); } echo factorial(5); // … Continue reading

Posted in PHP | Leave a comment

Switch Statement PHP (Simple Example)

$i = 1; switch($i) { case 0: echo ‘i equals 0′; break; case 1: echo ‘i equals 1′; break; default: echo ‘i equals the default value’; }

Posted in PHP | Leave a comment

Do-While Statement PHP (Simple Example)

$i = 0; do { echo $i++; } while ($i <= 10);

Posted in PHP | Leave a comment

While Statement in PHP (Simple Example)

$i = 0; while ($i <= 10) { echo $i++; }

Posted in PHP | Leave a comment

For Loop vs Foreach Loop in PHP (Simple Example)

// For Loop for ($i = 1; $i <= 10; $i++) { echo $i; } // Foreach Loop $array = array("sample" => "value", "sample2" => "value2", "sample3" => "value3"); foreach ($array as $key => $val) { echo $key  . ‘: … Continue reading

Posted in PHP | Leave a comment