10 Simple PHP Programs for Beginners with Step-by-Step Explanations

Looking to learn PHP? This guide covers 10 simple PHP programs designed for beginners, with easy-to-understand code and step-by-step explanations. From basic "Hello World" to form handling, this tutorial will help you grasp fundamental PHP concepts like loops, arrays, functions, and conditionals. Start coding in PHP with these hands-on examples!

Oct 14, 2024 - 17:43
 0

1. Hello World Program

<?php // This is a single-line comment echo "Hello, World!"; ?>

Explanation:

  • <?php: Starts the PHP code.
  • echo "Hello, World!";: Outputs the string "Hello, World!" to the screen.
  • ?>: Ends the PHP code (optional in some contexts).

2. Sum of Two Numbers

<?php
$a = 5;
$b = 10;
$sum = $a + $b;
echo "The sum of $a and $b is: $sum";
?>

Explanation:

  • $a = 5;: Assigns the value 5 to variable $a.
  • $b = 10;: Assigns the value 10 to variable $b.
  • $sum = $a + $b;: Adds $a and $b and stores the result in $sum.
  • echo "The sum of $a and $b is: $sum";: Outputs the sum of $a and $b.

3. Check Even or Odd

<?php
$number = 7;
if ($number % 2 == 0) {
          echo "$number is even";
} else
{
     echo "$number is odd";
}
?>

Explanation:

  • $number = 7;: Assigns the value 7 to $number.
  • if ($number % 2 == 0): Checks if $number is divisible by 2 (even).
  • echo "$number is even";: Outputs that the number is even.
  • else: Executes if the number is not even.
  • echo "$number is odd";: Outputs that the number is odd.

4. Simple Factorial Program

<?php
$num = 5;
$factorial = 1;
for ($i = $num; $i > 1; $i--) {
      $factorial *= $i;
}
echo "Factorial of $num is: $factorial";
?>

Explanation:

  • $num = 5;: Assigns 5 to $num.
  • $factorial = 1;: Initializes $factorial with 1.
  • for ($i = $num; $i > 1; $i--): Loops from $num down to 1.
  • $factorial *= $i;: Multiplies $factorial by $i in each iteration.
  • echo "Factorial of $num is: $factorial";: Outputs the factorial.

5. Simple Array and Loop

<?php
$fruits = array("Apple", "Banana", "Orange"); foreach ($fruits as $fruit) {
   echo $fruit . "<br>";
}
?>

Explanation:

  • $fruits = array("Apple", "Banana", "Orange");: Creates an array with fruit names.
  • foreach ($fruits as $fruit): Loops through each element in the $fruits array.
  • echo $fruit . "<br>";: Outputs each fruit with a line break.

6. String Length Function

<?php
$str = "Hello, PHP!";
echo "The length of the string is: " . strlen($str);
?>

Explanation:

  • $str = "Hello, PHP!";: Assigns a string to $str.
  • strlen($str): Returns the length of the string.
  • echo: Outputs the string length.

7. Check for Prime Number

<?php
$number = 29;
$is_prime = true;
for ($i = 2; $i <= sqrt($number); $i++) {
      if ($number % $i == 0) {
              $is_prime = false;
               break;
         }
}
if ($is_prime) {
           echo "$number is a prime number";
} else {
           echo "$number is not a prime number";
}
?>

Explanation:

  • $number = 29;: Assigns the number 29.
  • $is_prime = true;: Assumes $number is prime initially.
  • for ($i = 2; $i <= sqrt($number); $i++): Loops through possible divisors up to the square root of the number.
  • if ($number % $i == 0): Checks if $number is divisible by any $i. If so, the number is not prime.
  • if ($is_prime): Outputs the result based on whether $is_prime remains true.

8. Simple Calculator using Functions

<?php 
function add($a, $b) {
       return $a + $b;
}
function subtract($a, $b) {
       return $a - $b;
}
echo "Sum: " . add(10, 5) . "<br>";
echo "Difference: " . subtract(10, 5);
?>

Explanation:

  • function add($a, $b): Defines a function to add two numbers.
  • function subtract($a, $b): Defines a function to subtract two numbers.
  • echo "Sum: " . add(10, 5);: Calls the add function and outputs the result.
  • echo "Difference: " . subtract(10, 5);: Calls the subtract function and outputs the result.

9. Simple Associative Array

<?php
$person = array("name" => "John", "age" => 25, "gender" => "Male");
echo "Name: " . $person["name"] . "<br>";
echo "Age: " . $person["age"] . "<br>";
echo "Gender: " . $person["gender"];
?>

Explanation:

  • $person = array(...): Creates an associative array with key-value pairs.
  • echo "Name: " . $person["name"];: Accesses and outputs the value associated with the "name" key.
  • The same applies for "age" and "gender".

10. Form Handling

<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo     $_SERVER['PHP_SELF']; ?>">
    Name: <input type="text" name="name">
    <input type="submit">
</form>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
     $name = $_POST['name'];
     echo "Hello, $name!"; }
?>
</body>
</html>

Explanation:

  • <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">: Creates a form that sends data to the same page using the POST method.
  • if ($_SERVER["REQUEST_METHOD"] == "POST"): Checks if the form is submitted via POST.
  • $name = $_POST['name'];: Retrieves the submitted data from the form input.
  • echo "Hello, $name!";: Outputs the submitted name.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow