Essential PHP Programming Examples for Beginners

Posted by Anonymous and classified in Computers

Written on in with a size of 1.26 KB

Addition of Two Numbers

<?php
if(isset($_POST['n1']) && isset($_POST['n2'])){
    $n1 = $_POST['n1'];
    $n2 = $_POST['n2'];
    $res = $n1 + $n2;
    echo "<h3>User Input Numbers:</h3>";
    echo "First Number = $n1 <br>";
    echo "Second Number = $n2 <br>";
    echo "Sum = $res";
}
?>

Enter Two Numbers:





PHP Conditional, Loops and Arrays Example

<?php
echo "<h3>Loop Example (1 to 5):</h3>";
for($i=1;$i<=5;$i++){
    echo "Number: $i <br>";
}
echo "<h3>Array Example:</h3>";
$fruits = array("Apple","Banana","Mango","Orange");
foreach($fruits as $f){
    echo "$f<br>";
}
?>

Student Registration Form

Name:

Email:

Gender:
Male
Female

Subjects:
Math
Science
English

Course:
B.TechBCAB.Sc

Student Registration Details

<?php
// Assuming data is received via POST
echo "Name: $name <br>";
echo "Email: $email <br>";
echo "Gender: $gender <br>";
echo "Subjects: $subjects <br>";
echo "Course: $course <br>";
?>

Related entries: