July 11, 2025 By MarketingSA Off

PHP Statements and Language Constructs Overview

1. Output Statements

php

echo "Hello World";       // Outputs one or more strings
print "Hello";            // Outputs a string (returns 1, slower than echo)
printf("%s", $variable);  // Formatted output (like C's printf)

2. Variable Handling

php

$var = "Value";           // Variable assignment
unset($var);              // Destroy a variable
isset($var);              // Check if variable exists and is not null
empty($var);              // Check if variable is empty (0, "", null, false, [])

3. Control Structures

Conditionals:

php

if (condition) { ... } elseif (...) { ... } else { ... }
switch ($var) { case 1: ...; break; default: ... }

Loops:

php

while (condition) { ... }
do { ... } while (condition);
for ($i=0; $i<10; $i++) { ... }
foreach ($array as $key => $value) { ... }

Loop Control:

php

break;        // Exit loop/switch
continue;     // Skip current iteration

4. Function & Class Definitions

php

function myFunc() { ... }           // Define function
class MyClass { ... }               // Define class
trait MyTrait { ... }               // Define trait
interface MyInterface { ... }       // Define interface

5. Error Handling

php

try { ... } catch (Exception $e) { ... } finally { ... }
throw new Exception("Message");     // Throw exception
trigger_error("Message");           // Generate user-level error/warning

6. File Inclusion

php

include "file.php";      // Include file (warning if missing)
require "file.php";      // Require file (fatal error if missing)
include_once "file.php"; // Include only once
require_once "file.php"; // Require only once

7. Return & Exit

php

return $value;    // Return from function/method
exit;             // Terminate script execution
die("Message");   // Exit with message (alias of exit)

8. Namespace & Use

php

namespace My\Project;   // Declare namespace
use My\Project\Class;   // Import class/trait/interface
use function myFunction; // Import function
use const MY_CONST;     // Import constant

9. Special Language Constructs

php

__halt_compiler();     // Stop compiler execution
declare(ticks=1);      // Set directives for a block
goto label;             // Jump to label (avoid in practice)

10. Array & Object Operations

php

$arr = [1, 2, 3];              // Array declaration
$obj = new MyClass();           // Object instantiation
yield $value;                   // Generator return (in generator functions)

Example Snippet:

php

<?php
// File inclusion
require_once "config.php";

// Conditional + Loop
if ($loggedIn) {
    for ($i = 0; $i < 5; $i++) {
        echo "Count: $i\n";
    }
} else {
    die("Access denied!");
}

// Function + Exception
function divide($a, $b) {
    if ($b == 0) {
        throw new Exception("Division by zero");
    }
    return $a / $b;
}
?>

Key Notes:

  • PHP statements end with ; (semicolon).
  • echo/print are language constructs, not functions.
  • include/require are evaluated at runtime, while include_once/require_once prevent duplicate inclusion.
  • yield enables generator functions (memory-efficient iteration).

For detailed usage, refer to the PHP Manual.