I just began learning PHP. I've installed php5 on Linux and wrote very simple code just to get going.
How can I run scripts? I tried using the -f option, but it works as a cat command and just spits out the code to standard output.
The interactive interpreter option works fine. Is a web browser the only way to execute a PHP script?
A simple:
php myScript.php
… should do the job.
If it is acting like cat, then you probably forgot to switch out of template mode and into script mode with <?php
Shorter way for command line:
php -r 'echo "Hello "; echo "Jay";'
OR
php -r 'echo dirname("parent/child/reply") . "\n";'
As already mentioned, you can execute your PHP with the following.
php myScript.php
If you wish to pass an argument(s), you can simply do so like this:
php myScript.php Apples
In your PHP file you can use this argument by accessing the $argv array like this:
<?php
echo 'I like ' . $argv[1];
?>
The above would print our "I like Apples".
Note the array index is 1 and not 0. 0 is used for script name. In this case $argv would be "myScript.php"
For more information, check out my blog post Running PHP from the Command Line - Basics.
Actually, PHP's main purpose is to generate web pages, but there are at least two other options:
command line (CLI) script execution,
interactive shell - which is actually the variant of the previous option,
The first one can be achieved in many ways (eg. by giving proper permissions to the file and calling script by providing its URI, eg. ./index.php), the second one can be invoked by php -a command (as stated in the documentation mentioned above).
Related
I am calling a shellscript from php using shell_exec() command. Following is the simple version of the shell script:
args[0] = "tom"
echo "hello"
echo "${args[0]}"
When I run this script from terminal, it gives the following output in terminal:
hello
tom
Whereas when I call this from php using shell_exec() only "hello" is printed and not "tom" . ie; variable assignment is not working when the script is called from php. Why this happens and how can I resolve this.
Any help is appreciated.
Probably PHP executes the script with sh, not Bash; thus arrays (which are a Bash feature) are not supported by the shell.
Workarounds: don't use arrays, or explicitly inboke Bash on the script. (If PHP understands shebangs, having a correct shebang line as the first line of the script may well be sufficient.)
First, args[0] = "tom" should not have space. It should be args[0]="tom"
(I'm not sure is this the problem, but worth to try).
Try this at the end of your code, to see which shell is running your script.
echo "`ps -p $$`"
Try both on terminal and PHP scripts to see if it same shell or not.
Is there a correct way to use php from the command line...or rather...is one way more correct than another ?
If you create a file, say test.php with the following code:
#!/usr/bin/php
<?php
print "This is a test".PHP_EOL;
print "This is another test!!";
?>
then chmod +x text.php (make it executable on linux).
You can then run in the following ways.....
./test.php
or
php test.php
I prefer just using ./test.php, but often see php test.php in examples.
ALSO
is the following correct syntax for the shebang line
#!/usr/bin/php
or is this more correct
#!/usr/bin/php -q
I've seen both, and see that the -q flag is to quiet the html stuff, but was wondering if
php compiled with cli compatibility really needs the -q flag ???
Thanks for your help :)
#!/usr/bin/php
On the first line of an interpreter script, the "#!", is the name of a program which should be used to interpret the contents of the file. For instance, if the first line contains
"#! /bin/sh"
, then the contents of the file are executed as a shell script.
In your case it means excute the script using the php file in /usr/bin location.
Using php test.php means that you are running your test.php with php so u don't need the first line. where as for ./test.php your file needs to be executable and first line is required.
And about the -q flag look at this
specifically on
rob 23-Mar-2007 11:48
There are better expanations if you see for -q. here's another one
If you are writing little php scripts for your own purposes, #! is fine.
If they are to be on some kind of world visible box (e.g. web server), then I'd say not so fine - since you have made them executable they are now a security risk.
I tend to use #! for perl, sh (which are always little private, non production things) and php somefile.php for PHP (which may or may not end up on a server).
I have a script that uses passthru() to run a command. I need to set some shell environment variables before running this command, otherwise it will fail to find it's libraries.
I've tried the following:
putenv("LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
Using putenv() doesn't appear to propagate to the command I'm running. It fails saying it can't find it libraries. When I run export LD_LIBRARY_PATH=/path/to/lib in bash, it works fine.
I also tried the following (in vain):
exec("export LD_LIBRARY_PATH=/path/to/lib");
passthru($cmd);
How can I set a shell variable from PHP, that propagates to child processes of my PHP script?
Am I limited to checking if a variable does not exist in the current environment and asking the user to manually set it?
I'm not 100% familiar with how PHP's exec works, but have you tried: exec("LD_LIBRARY_PATH=/path/to/lib $cmd")
I know that this works in most shells but I'm not sure how PHP doing things.
EDIT: Assuming this is working, to deal with multiple variables just separate them by a space:
exec("VAR1=val1 VAR2=val2 LD_LIBRARY_PATH=/path/to/lib $cmd")
You could just prepend your variable assignments to $cmd.
[ghoti#pc ~]$ cat doit.php
<?php
$cmd='echo "output=$FOO/$BAR"';
$cmd="FOO=bar;BAR=baz;" . $cmd;
print ">> $cmd\n";
passthru($cmd);
[ghoti#pc ~]$ php doit.php
>> FOO=bar;BAR=baz;echo "output=$FOO/$BAR"
output=bar/baz
[ghoti#pc ~]$
A couple things come to mind. One is for $cmd to be a script that includes the environment variable setup before running the actual program.
Another thought is this: I know you can define a variable and run a program on the same line, such as:
DISPLAY=host:0.0 xclock
but I don't know if that work in the context of passthru
https://help.ubuntu.com/community/EnvironmentVariables#Bash.27s_quick_assignment_and_inheritance_trick
I have this PHP script (test.php):
<?php
$cmd = "/usr/bin/sass --watch file1.scss";
system($cmd);
?>
Now I call my PHP script from CLI this way:
/usr/bin/php test.php
And I get no output (it should print SASS is watching for changes).
If I call the SASS command directly from the shell, it outputs correctly.
Why?
Info: I'm using the PHP 5.3.6 version on OS X Lion
Edit: Please, note that this command watches for changes, it seems to behave differently to a regular command.
Edit2: The command works, it compiles correctly. The only thing lacking is the output (I want to debug and see errors :)
Some command line utilities like sass, manipulate the output pipe in some way that PHP can't use.
So, in this particular case, there is no solution.
system() returns a string. Just echo it.
According to http://se.php.net/system you need to pass in a second argument to system() and the return value of the command will be set in that variable:
<?php
system($command, $return);
echo $return;
Using Python I can test my code in the terminal / command line by typing
python
python> print "hello world"
I would like to do this with PHP too, but when typing:
php
echo "hello world";
it does not work.. Is this possible? what should I do?
A quick search on the internet gives a lot of results that call an actual .php file to run. I only want to test a single sentence if possible, without creating files and stuff.
Try
php -a
which starts an interactive PHP shell. Be aware that this requires PHP to be built with --with-readline (which is not the case if you're using the bundeled PHP with Mac OS X e.g.).
Alternatively, if you don't require the interactivity of a separate shell, use
php -r 'print_r(get_defined_constants());'
to execute a PHP snippet (this doesn't require the readline support).
php -r "echo 'hello world';"
If you run php without the -a option, don't forget the <?php at the start