Hudzilla.org - the homepage of Paul Hudson
Contents > Simple variables and operators > Operators Wish List | Report Bug | About Me ]

3.12.6     The execution operator

This is NOT the latest copy of this book; click here for the latest version.

The least complicated of all three special operators is the execution operator, which is ` - a back tick. Back ticks are used very rarely in normal typing, so you might have trouble finding where yours is - it is almost certainly to the left of your 1 key on your keyboard. It may well share a key with other obscure symbols such as ¬.

The execution operator in PHP is very clever - it allows you to pass commands direct to the operating system for execution, then capture the results. PHP literally replaces the result of the execution with what you asked to be executed. For example:

print `ls`;

That will run the command "ls" and output its results to the screen. If you are using Windows, you will need to use "dir" instead, as ls is only available on Unix. You can perform any commands inside back ticks that you would normally perform direct from the command line, including piping output to and from and/or redirecting output through other programs.

There are several functions that perform program execution like the execution operator - you can find a more comprehensive reference to them in the Functions chapter.





<< 3.12.5 The scope resolution operator   3.12.7 Operator precedence and associativity >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Ravn Revheim - 29 Aug 2008

On a norwegian keyborad, and I'm guessing on danish and swedish as well, it is located to the left of the backspace key.

Gus jones - 29 Aug 2008

With safemode on the backtips don't seem to work. I get this error message:

Warning: shell_exec() [function.shell-exec]: Cannot execute using backquotes in Safe Mode in
C:\Program Files\xampp\htdocs\gus_jones\testinfo.php on line 77

Fine with me...

A PHP User - 29 Aug 2008

hum... executing shell commands inside scripts seems dangerous to me. Imagine your users putting commands like "w", "ps -ef", "netstat -rn", "ls /" and so on inside PHP scripts in their public_html directories! How do i disable this feature?

thnx!

A macintosh User - 29 Aug 2008

On my Apple Macintosh PowerBook Laptop running OS X it is the same as the Tilde "~" button left of the one. The previous poster might have an older keyboard.

A PHP User - 29 Aug 2008

lol, man \n is for console, and while <br> is working for you, u are viewing your scripts via browser.

second not print 'dir';
but print `dir`;

anyway as mentioned before you should check your php configuration - is safe mode on? well then u can use shell_execute() or what was the name...

o@jonchrist.org - 29 Aug 2008

Hi,
thanks for this great ebook!
On my localhost (Apache-Server 2.0 runs in WIN XP) neither "/n" (helped myself with <br>) etc. nor e.g. "<?php print 'dir'; ?>" do something. Is this because of adjustments of the localhost (Apache 2.0)?
Pls give the Dummy (me!) a hint. Thanks in advance.

Oliver from DE-Hamburg ;)

Dvorak user - 29 Aug 2008

On Kinesis Dvorak keyboard, this is the key under the QWERTY "Z" or the Dvorak ";".

A PHP User, Navi - 29 Aug 2008

Beware of safe mode - usully on most hostings you just cannot use 'executing' of commands - which might differ from your "localhost" settings at home.

Igor - 29 Aug 2008

Happy Mac users should look for back tick left of "Z" key. :-D



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is ten plus nine?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow