PHP

1.string handling

<!DOCTYPE html>

<html>

<head>

 <meta charset="utf-8">

 <meta name="viewport" content="width=device-width, initial-scale=1">

 <title></title>

</head>

<body>

 <style type="text/css">

 table tr td {

padding:5px;

}

</style>

<?php

$name="Welcome to KGISL IIM!";

echo "<center style='font-size:20px'>";

echo "<h3>Input String: ".$name ."</h3>";

echo "<br><br>";

echo "<table border=1> <th>Method </th> <th> Syntax </th> <th>Result </th>";

echo "<tr><td>String Length</td> <td>strlen(input_string) </td> <td> ".strlen($name)."</td></tr>";

echo "<tr><td>Word Count</td> <td>str_word_count(input_string)</td> <td>

".str_word_count($name)."</td> </tr>";

echo "<tr><td>Word Position</td> <td>strpos(input_string,position)</td> <td> ".strpos($name,


"IIM")."</td> </tr>";

echo "<tr><td>Word Reverse</td><td>strrev(input_string)</td> <td> ".strrev($name)."</td>

</tr>";

echo "<tr><td>Trim</td><td>trim(input_string,replaced_word)</td> <td>

".trim($name,"IIM!")."</td> </tr>";

echo "<tr><td>Right Trim</td><td>ltrim(input_string,replaced_word)</td> <td>

".rtrim($name,"IIM!")."</td> </tr>";

echo "<tr><td>Left Trim</td><td>ltrim(input_string,replaced_word)</td> <td>

".ltrim($name,"Welcome")."</td> </tr>";

echo "<tr><td>MD5 Hash</td><td>md5(input_string)</td> <td> ".md5($name)."</td> </tr>";

echo "<tr><td>Repate word</td><td>str_repeat(input_string,repeated_word)</td> <td>

".str_repeat($name,2)."</td> </tr>";

echo "<tr><td>Shuffle Characters</td><td>str_shuffle(input_string)</td> <td>

".str_shuffle($name)."</td> </tr>";

echo "</table>";

echo "</center>";

?>

</body>

</html>


2.Associative array 


PROGRAM


<?php

$students = array(

    array(

        "name" => "Raja",

        "marks" => array(90, 90, 85, 92, 88)

    ),

    array(

        "name" => "Dinesh",

        "marks" => array(75, 88, 92, 79, 83)

    ),

    array(

        "name" => "Arun",

        "marks" => array(92, 87, 90, 94, 91)

    )

);

?>


<!DOCTYPE html>

<html>

<head>

    <style>

        table {

            border-collapse: collapse;

            width: 100%;

        }


        th, td {

            padding: 8px;

            text-align: left;

            border-bottom: 1px solid #ddd;

        }

        th {

            background-color: lightgrey;

        } 

    </style>

</head>

<body>

<h1>Student Marks using Associative Array</h1>

    <table>

        <tr>

            <th>Name</th>

            <th>PHP</th>

            <th>Cloud Computing</th>

            <th>Software Testing</th>

            <th>Python</th>

            <th>NSC</th>

        </tr>

        <?php foreach ($students as $student) { ?>

            <tr>

                <td><?php echo $student["name"]; ?></td>

                <?php foreach ($student["marks"] as $mark) { ?>

                    <td><?php echo $mark; ?></td>

                <?php } ?>

            </tr>

        <?php } ?>

    </table>

</body>

</html>


3.Arithmetic Operations 

<?php  


// Function to display the heading

function writeHeading() {  

    echo "<h2>Arithmetic Operations Using Different Functions</h2> <br>";      

}

writeHeading();


// Function for addition with default arguments

function add($x = 10, $y = 20, $z = 30) {  

    echo "Sum using Default arguments (x=10, y=20, z=30): " . ($x + $y + $z) . "<br><br>";

}


// Function for subtraction

function sub($x, $y) {  

    return $x - $y;

}


// Function for multiplication using call by reference

function mul(&$x, $y) {  

    $x *= $y;

}


// Function for division using return array

function div($dividend, $divisor) {

    return [intdiv($dividend, $divisor), $dividend % $divisor];

}


// Handle form submissions

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $first = $_POST['first'];

    $second = $_POST['second'];


    if (isset($_POST['add'])) add($first, $second);

    if (isset($_POST['sub'])) echo "Difference = " . sub($first, $second) . "<br><br>";

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

        mul($first, $second);

        echo "Multiplication = $first <br><br>";

    }

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

        list($quotient, $reminder) = div($first, $second);

        echo "Quotient: $quotient<br>Reminder: $reminder<br><br>";

    }

?>  


<form method="post">  

    Enter first number: <input type="number" name="first" required /><br><br>  

    Enter second number: <input type="number" name="second" required /><br><br>

    <input type="submit" name="add" value="ADDITION"/>  

    <input type="submit" name="sub" value="SUBTRACTION"/>

    <input type="submit" name="mul" value="MULTIPLICATION"/>

    <input type="submit" name="div" value="DIVISION"/>  

</form>

Comments

Popular posts from this blog

PHP 4 to 10

2,4