• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

How to Execute PHP Codes in Linux through Command Line

{{postValue.id}}

Steps to Use and Execute PHP Codes in Linux Command Line

PHP is designed for web development which is used as a general-purpose programming language. It is a server side scripting language. We can execute a PHP in a Linux terminal without using a web browser.


Install PHP command Line Interpreter

After the installation of PHP and Apache2, install PHP command Line Interpreter

On Debian Based Systems

# apt-get install php5-cli

On CentOS Based systems

[root@linuxhelp ~]# yum install php-cli
Loaded plugins: auto-update-debuginfo, fastestmirror, refresh-packagekit, : security
Setting up Install Process
Determining fastest mirrors
epel/metalink                                            | 4.4 kB     00:00     
epel-debuginfo/metalink                                  | 4.6 kB     00:00     
epel-source/metalink                                     | 4.4 kB     00:00     
 * base: centos.excellmedia.net
.
.
.
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : php-cli-5.3.3-46.el6_7.1.x86_64                              1/1
Verifying  : php-cli-5.3.3-46.el6_7.1.x86_64                              1/1

Installed:
php-cli.x86_64 0:5.3.3-46.el6_7.1                                             

Complete!

Now the installation is completed successfully.


To create a simple PHP file

To check if php is working or not, create a sample PHP file.

[root@linuxhelp ~]#  vi /var/www/html/sample.php

To obtain the output, run the PHP file in the following location ' /var/www/html/sample.php' in the Linux Command Line.

[root@linuxhelp ~]#  php -f /var/www/html/sample.php
Welcome to php

' -f ' option is used to parse and execute the file.

2. To do some mathematics, run PHP in Interactive mode by using the option ' -a' .

[root@linuxhelp ~]#  php &ndash a
Interactive shell
Php  >   echo   7*8   
56
 Php  >   echo   6+4   
10
Php  >   echo   15-2   
13
Php  >   echo   14/2   
7
Php  >   echo   3*5+5-2   
18
Php  >   exit

To close this PHP interactive mode press ' exit' or enter ' ctrl+c' .

4. It is simple to run a PHP script, if we use shell script. Create a PHP sample script in the current working directory.

[root@linuxhelp ~]# cat addition.php

[root@linuxhelp ~]#  php -f addition.php
15

5. By using the interactive shell, we can create simple functions by our self. Step by step instruction is given below.


To start PHP interactive mode

[root@linuxhelp ~]# php&ndash a
Interactive shell
Php>  $a=7  
Php>  $b=8  
Php>  $c=$a+$b  
Php> echo “ $c
”   
15
echo>  exit

Once the interactive shell is exit, the user defined functions will not save in history from shell session to shell session.

Tags:
elijah
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

How you prompted to php console?

A

"php -a" is used for it

Q

Please proceed with the work !

A

Thank you for your comment !!

Q

What version of php you have used in your post?

A

Default version 5.3.10 that comes with the installation

Q

Where the sample script created why particularly in the provided path "var/www/html/"

A

Here I wanted to show my output in web interface so I have created a sample php code under Apache webserver document root

Q

How to install and Configure Server Explorer on Cpanel?

A

Refer the following link to know configure server explorer on cpanel

https://www.linuxhelp.com/how-to-install-config-server-explorer-on-whm/

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Sebastian ?
How to change non required to required field in SuiteCRM Custom/Default Modules

How to change not required to the required field in SuiteCRM Custom/Default Modules?

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.