,

Get information about your memory and CPU usage in PHP

 In this tutorial, I am going to share how you can get information about your memory and CPU usage in PHP, optimizing server memory is a big problem and you may have to face server downtime issues.

PHP has a garbage collector and a pretty complex memory manager. The amount of memory being used by your script. can go up and down during the execution of a script. To get the current memory usage, we can use the memory_get_usage() function, and to get the highest amount of memory used at any point, we can use the memory_get_peak_usage() function.

Code

echo "Initial Memory uses : ".memory_get_usage()." bytes \n";
// Initial Memory uses : 321420 bytes

// Let's create some function to use up some memory
for ($count = 0; $count < 100000; $count++) {
$array []= base64_decode($count);
} 
for ($count = 0; $count < 100000; $count++) {
unset($array[$i]);
}
echo "Final Memory : ".memory_get_usage()." bytes \n";
//Final Memory :: 871015 bytes
 
echo "Peak: ".memory_get_peak_usage()." bytes \n";
//Peak: 13483071 bytes
Continue reading Get information about your memory and CPU usage in PHP
, ,

How to get multiple selected values of select box in php?

 Given a list of items and the task is to retrieve the multiple selected value from a select box using PHP.

Use multiple attribute in HTML to select multiple value from drop down list. Selecting multiple values in HTML depends on operating system and browser. 
 

  • For window users – hold down + CTRL key to select multiple option
  • For mac users – hold down command key to select multiple option

Example: This example creates a list of items using HTML. 
 

<html>
    <body>
        <form method = "post" action = "name.php">
            <h4>SELECT SUBJECTS</h4>
            <!--Using multiple to select multiple value-->
            <select name = "subject" multiple size = 6
                <option value = "english">ENGLISH</option>
                <option value = "maths">MATHS</option>
                <option value = "computer">COMPUTER</option>
                <option value = "physics">PHYSICS</option>
                <option value = "chemistry">CHEMISTRY</option>
                <option value = "hindi">HINDI</option>
            </select>
            <input type = "submit" name = "submit" value = Submit>
        </form>
    </body>
</html>

Now, the task is to retrieve or print multiple selected value from list. Use form method and loop to retrieve selected value in PHP.
Example: 
 

<html>
    <body>
        <!--name.php to be called on form submission-->
        <form method = 'post'>
            <h4>SELECT SUBJECTS</h4>
             
            <select name = 'subject[]' multiple size = 6> 
                <option value = 'english'>ENGLISH</option>
                <option value = 'maths'>MATHS</option>
                <option value = 'computer'>COMPUTER</option>
                <option value = 'physics'>PHYSICS</option>
                <option value = 'chemistry'>CHEMISTRY</option>
                <option value = 'hindi'>HINDI</option>
            </select>
            <input type = 'submit' name = 'submit' value = Submit>
        </form>
    </body>
</html>
<?php
     
    // Check if form is submitted successfully
    if(isset($_POST["submit"]))
    {
        // Check if any option is selected
        if(isset($_POST["subject"]))
        {
            // Retrieving each selected option
            foreach ($_POST['subject'] as $subject)
                print "You selected $subject<br/>";
        }
    else
        echo "Select an option first !!";
    }
?>

Output: 
 

image

Note: The form can be submitted using $_GET method. It depends on the form method=”?” value.

Continue reading How to get multiple selected values of select box in php?