Showing posts with label php. Show all posts
Showing posts with label php. Show all posts
,

Installing PHP Composer on CentOS 8 [Quick Way]

 Perform the following steps to quickly install Composer on your CentOS 8 system:


  1. Install PHP CLI and Zip:

    $ sudo dnf install php-cli php-json php-zip curl unzipCopy
  2. Download Composer with curl:

    $ curl -sS https://getcomposer.org/installer |php
  3. Move the Composer file to /usr/local/bin directory:

    $ sudo mv composer.phar /usr/local/bin/composer
Continue reading Installing PHP Composer on CentOS 8 [Quick Way]
, , ,

How to install PHP 8 on CentOS 8 Linux

 This tutorial will explain how to install PHP 8 on CentOS 8 Linux and its dependent packages.



Step 1: Enableremi-release-8.rpm

Configure the Remi repository for installing PHP 8 on CentOS 8

# dnf install https://rpms.remirepo.net/enterprise/remi-release-8.rpm

Step 2: Enable EPEL repository

# dnf install epel-release

Step 3: Find the current enabled version of PHP

# dnf module list php
CentOS Stream 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 common [d], devel, minimal PHP scripting language

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

Step 4: Enable the module PHP8.1 from remi

From the above output, by default PHP 7.2 from the AppStream repo shall be installed and no other version is enabled. To install PHP 8.1 from the remi repo, enable it before installation.

# dnf module enable php:remi-8.1
# dnf module list php
CentOS Stream 8 - AppStream
Name Stream Profiles Summary
php 7.2 [d] common [d], devel, minimal PHP scripting language
php 7.3 common [d], devel, minimal PHP scripting language
php 7.4 common [d], devel, minimal PHP scripting language
php 8.0 common [d], devel, minimal PHP scripting language

Remi's Modular repository for Enterprise Linux 8 - x86_64
Name Stream Profiles Summary
php remi-7.2 common [d], devel, minimal PHP scripting language
php remi-7.3 common [d], devel, minimal PHP scripting language
php remi-7.4 common [d], devel, minimal PHP scripting language
php remi-8.0 common [d], devel, minimal PHP scripting language
php remi-8.1 [e] common [d], devel, minimal PHP scripting language
Switch to the other version?

In case, if you need to install any other version, you can reset the current module enabled and set the new using the below commands:

# dnf module reset php

# dnf module enable php:remi-8.0

Step 5: Install PHP 8 on CentOS 8

# dnf install php

Last metadata expiration check: 0:13:44 ago on Wed 16 Feb 2022 11:53:53 AM IST.
Dependencies resolved.
=============================================================================================================================================================================================================================================
Package Architecture Version Repository Size
=============================================================================================================================================================================================================================================
Installing:
php x86_64 8.1.2-1.el8.remi remi-modular 1.7 M
Installing dependencies:
apr x86_64 1.6.3-12.el8 appstream 129 k
apr-util x86_64 1.6.1-6.el8 appstream 105 k
centos-logos-httpd noarch 85.8-2.el8 baseos 75 k
httpd x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1 appstream 1.4 M
httpd-filesystem noarch 2.4.37-43.module_el8.5.0+1022+b541f3b1 appstream 39 k
httpd-tools x86_64 2.4.37-43.module_el8.5.0+1022+b541f3b1 appstream 107 k
libsodium x86_64 1.0.18-2.el8 epel 162 k
libxslt x86_64 1.1.32-6.el8 baseos 250 k
mailcap noarch 2.1.48-3.el8 baseos 39 k
mod_http2 x86_64 1.15.7-3.module_el8.4.0+778+c970deab appstream 154 k
oniguruma5php x86_64 6.9.7.1-1.el8.remi remi-safe 210 k
php-common x86_64 8.1.2-1.el8.remi remi-modular 1.2 M
Installing weak dependencies:
apr-util-bdb x86_64 1.6.1-6.el8 appstream 25 k
apr-util-openssl x86_64 1.6.1-6.el8 appstream 27 k
nginx-filesystem noarch 1:1.14.1-9.module_el8.0.0+184+e34fea82 appstream 24 k
php-cli x86_64 8.1.2-1.el8.remi remi-modular 5.3 M
php-fpm x86_64 8.1.2-1.el8.remi remi-modular 1.8 M
php-mbstring x86_64 8.1.2-1.el8.remi remi-modular 522 k
php-opcache x86_64 8.1.2-1.el8.remi remi-modular 628 k
php-pdo x86_64 8.1.2-1.el8.remi remi-modular 160 k
php-sodium x86_64 8.1.2-1.el8.remi remi-modular 100 k
php-xml x86_64 8.1.2-1.el8.remi remi-modular 250 k
Enabling module streams:
httpd 2.4
nginx 1.14

Transaction Summary
=============================================================================================================================================================================================================================================
Install 23 Packages

Step 6: Verify the PHP installation

# php -v
PHP 8.1.2 (cli) (built: Jan 18 2022 23:52:03) (NTS gcc x86_64)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2, Copyright (c), by Zend Technologies
Enjoy and comment if any issue arrised...
Continue reading How to install PHP 8 on CentOS 8 Linux
,

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?