PHP

 1. Prime Numbers



<?php

function isPrime($n) {

    if ($n <= 1)

        return false;

    for ($i = 2; $i <= sqrt($n); $i++) {

        if ($n % $i == 0)

            return false;

    }

    return true;

}


$start = 3;

$end = 20;

$count = 0;


for ($i = $start; $i <= $end; $i++) {

    if (isPrime($i)) {

        echo "$i \t";

        $count++;

    }

}


echo "<br>The Total number of Prime numbers are = $count";

?>








2. Message Passing (Form and Receiver Page)


form.html:



<html>

<style>

    body { background-color: lightblue; }

</style>

<center>

    <form action="p2p.php" method="post">

        Enter the Message:

        <input type="text" name="msg"><br><br>

        <input type="submit" value="Send Message">

    </form>

</center>

</html>




p2p.php:



<html>

<style>

    body { background-color: lightblue; text-align: center; }

</style>

<body>

<?php

if (isset($_POST['msg'])) {

    $message = htmlspecialchars($_POST['msg']);

    echo "The sent message is: $message";

} else {

    echo "No message received.";

}

?>

</body>

</html>





3. Simple Calculator


<?php

$ans = '';

if ($_POST) {

    $number1 = $_POST['n1'];

    $number2 = $_POST['n2'];

    $op = $_POST['submit'];


    if (is_numeric($number1) && is_numeric($number2)) {

        if ($op == '+')

            $ans = $number1 + $number2;

        else if ($op == '-')

            $ans = $number1 - $number2;

        else if ($op == 'x')

            $ans = $number1 * $number2;

        else if ($op == '/')

            $ans = $number2 != 0 ? $number1 / $number2 : 'Error: Division by zero';

    } else {

        $ans = "Invalid input!";

    }

}

?>


<html>

<style>

    body { background-color: lightblue; font-weight: bolder; }

    #f { width: 200px; border-radius: 5px; }

    input { width: 50px; border-radius: 5px; }

</style>

<center>

    <form method="post">

        <h1>Simple Calculator</h1>

        First Number: <input type="text" name="n1" id="f"><br><br>

        Second Number: <input type="text" name="n2" id="f"><br><br>

        <input type="submit" name="submit" value="+">

        <input type="submit" name="submit" value="-">

        <input type="submit" name="submit" value="x">

        <input type="submit" name="submit" value="/"><br><br>

        Result: <input type="text" value="<?php echo $ans; ?>" readonly id="f">

    </form>

</center>

</html>





4. String Functions


<?php

$str1 = "First Programming language is Algorithm for the Analytical Engine";

$str2 = "first programmer was a woman \t";


echo "<br><br>String 1: " . $str1;

echo "<br><br>String 2: " . $str2;


echo "<br><br>Length of string 1: " . strlen($str1);

echo "<br><br>Total words in string 1: " . str_word_count($str1);

echo "<br><br>Uppercase of string 2: " . ucwords($str2);

echo "<br><br>Reverse of string 2: " . strrev($str2);

echo "<br><br>Substring of string 1 (1-17): " . substr($str1, 1, 17);

echo "<br><br>Repeated string 2 (2 times): " . str_repeat($str2, 2);

?>






5. Array Functions



<?php

$arr = array(10,9,8,7,6,5,4,3,2,1);

echo "<br><br>Original Array: ";

foreach ($arr as $x) echo "$x ";


echo "<br><br>Sum of Array elements: " . array_sum($arr);

echo "<br><br>Minimum value: " . min($arr);

echo "<br><br>Maximum value: " . max($arr);


echo "<br><br>Sorted Array: ";

sort($arr);

foreach ($arr as $x) echo "$x ";


echo "<br><br>Reverse Sorted Array: ";

rsort($arr);

foreach ($arr as $x) echo "$x ";


echo "<br><br>Array after pop: ";

array_pop($arr);

foreach ($arr as $x) echo "$x ";


echo "<br><br>Array after push 100: ";

array_push($arr, 100);

foreach ($arr as $x) echo "$x ";

?>






6. GCD and LCM Calculator


php


<?php

$gcd = '';

$lcm = '';

$error = '';


if ($_POST) {

    $a = $_POST['n1'];

    $b = $_POST['n2'];


    if (is_numeric($a) && is_numeric($b)) {

        $a = (int)$a;

        $b = (int)$b;


        function GCD($a, $b) {

            if ($b == 0) {

                return $a;

            } else {

                return GCD($b, $a % $b);

            }

        }


        $gcd = GCD($a, $b);

        $lcm = ( $b) / $gcd;

    } else {

        $error = "Please enter valid numbers only.";

    }

}

?>


<html>

<head>

    <style>

        body { background-color: lightblue; font-family: Arial, sans-serif; }

        .centered { text-align: center; margin-top: 50px; }

        input { margin: 5px; padding: 5px; border-radius: 5px; width: 200px; }

    </style>

</head>

<body>

    <div class="centered">

        <form method="post">

            <h1>GCD and LCM Calculator</h1>

            <label>Enter the First Number</label><br>

            <input type="text" name="n1"><br>

            <label>Enter the Second Number</label><br>

            <input type="text" name="n2"><br>

            <input type="submit" value="Calculate"><br><br>


            <?php if ($error): ?>

                <p style="color:red;"><?php echo $error; ?></p>

            <?php else: ?>

                GCD: <input type="text" value="<?php echo $gcd; ?>" readonly><br>

                LCM: <input type="text" value="<?php echo $lcm; ?>" readonly><br>

            <?php endif; ?>

        </form>

    </div>

</body>

</html>





SMALLER VIRSION:



<html>

<center>


<form method="post">

    First Number: <input name="n1"><br>

    Second Number: <input name="n2"><br>

    <input type="submit" value="Calculate">

</form>

<body>

<?php


if ($_POST) {

    $a = $_POST['n1'];

    $b = $_POST['n2'];


    if (is_numeric($a) && is_numeric($b)) {

        function gcd($a, $b) {

            return $b ? gcd($b, $a % $b) : $a;

        }

        $g = gcd($a, $b);

        $l = ($a * $b) / $g;

        echo "GCD: $g<br>LCM: $l";

    } else {

        echo "Enter valid numbers.";

    }


}

?>

</body>


</center>

</html>








7. Constructor and Destructor Example



<?php

class Add {

    public $a = 0;

    public $b = 0;


    function __construct() {

        $this->a = 10;

        $this->b = 20;

        echo "Object created.<br>";

    }


    function sum() {

        $sum = $this->a + $this->b;

        echo "Total = $sum<br>";

    }


    function __destruct() {

        echo "Object is destroyed.";

    }

}


$obj = new Add();

$obj->sum();

unset($obj);

?>







8. Time Zone Greeting


<?php

date_default_timezone_set("Asia/Calcutta");

$hour = date("G");


echo "The current time is (24-hour format): " . $hour . "<br>";


if ($hour < 12) {

    echo "Good morning";

} else if ($hour >= 12 && $hour < 18) {

    echo "Good afternoon";

} else if ($hour >= 18 && $hour <= 21) {

    echo "Good evening";

} else {

    echo "Good night";

}

?>







9. File Open and Copy Content



<?php  

$fp1 = fopen("Darshan.txt", "r");  


if (!$fp1) {

    echo "File does not exist.";

} else {

    $fp2 = fopen("style2.txt", "w");  

    fwrite($fp2, "Results (Darshan.txt)\n");  

    

    while (($ch = fgetc($fp1)) !== false) {  

        fputcsv($fp2, [$ch]);  

    }  

    

    fclose($fp1);                                

    fclose($fp2);


    echo "File copied successfully.";

}

?>


Comments

Popular posts from this blog