Category: Examples

  • File Handling

    <?php
    $file = fopen("example.txt", "w");
    fwrite($file, "Hello File Handling!");
    fclose($file);
    
    $file = fopen("example.txt", "r");
    echo fread($file, filesize("example.txt"));
    fclose($file);
    ?>
    

    Explanation:

    • "w" → write mode (creates/overwrites file).
    • "r" → read mode.
    • fwrite writes text into file, fread reads it.
  • String Functions

    <?php
    $text = " Hello PHP! ";
    
    echo strlen($text);    // 11
    echo strtolower($text); // hello php!
    echo strtoupper($text); // HELLO PHP!
    echo trim($text);       // removes extra spaces
    echo str_replace("PHP", "World", $text); // Hello World!
    ?>
    

    Explanation:
    PHP has many string functions for modifying and formatting text.

  • Switch Case Example

    <?php
    $day = "Monday";
    
    switch ($day) {
    
    case "Monday":
        echo "Start of the week!";
        break;
    case "Friday":
        echo "Weekend is near!";
        break;
    default:
        echo "Just another day.";
    } ?>

    Explanation:

    • switch is an alternative to multiple if-else statements.
    • If $day = "Monday", output will be Start of the week!
  • Connecting PHP with MySQL

    <?php
    $conn = new mysqli("localhost", "root", "", "testdb");
    
    if ($conn->connect_error) {
    
    die("Connection failed: " . $conn-&gt;connect_error);
    } echo "Connected successfully!"; ?>

    Explanation:

    • mysqli is used to connect PHP with MySQL database.
    • Replace "localhost", "root", "", "testdb" with your DB credentials.
  • Form Handling

    Create a file form.html:

    <form method="GET" action="welcome.php">
      Name: <input type="text" name="username">
      <input type="submit">
    </form>
    

    In welcome.php:

    <?php
    echo "Welcome " . $_GET["username"];
    ?>
    

    Explanation:

    • When you submit the form, username value is sent to welcome.php.
    • Example: If you type Saim, it shows Welcome Saim.
  • Associative Arrays

    <?php
    $person = [
    
    "name" =&gt; "Ali",
    "age" =&gt; 30,
    "city" =&gt; "Karachi"
    ]; echo $person["name"]; // Ali ?>

    Explanation:

    • Associative arrays use key-value pairs.
    • You can access values using their keys.
  • Functions Example

    <?php
    function greet($name) {
    
    return "Hello, $name!";
    } echo greet("Saim"); ?>

    Explanation:

    • Functions are created with function.
    • You can pass parameters ($name).
    • Output: Hello, Saim!
  • Arrays Example

    <?php
    $fruits = ["Apple", "Banana", "Orange"];
    
    foreach ($fruits as $fruit) {
    
    echo $fruit . "&lt;br&gt;";
    } ?>

    Output:

    Apple  
    Banana  
    Orange
  • While Loop

    <?php
    $i = 1;
    while ($i <= 3) {
    
    echo "Count: $i &lt;br&gt;";
    $i++;
    } ?>
  • Loops Example

    (a) For Loop

    <?php
    for ($i = 1; $i <= 5; $i++) {
    
    echo "Number: $i &lt;br&gt;";
    } ?>

    Output:

    Number: 1  
    Number: 2  
    Number: 3  
    Number: 4  
    Number: 5