PHP command in Bash does not execute code - php

Ok, so I have a problem with this thing I found on a MacBook Air. It's called terminal and you can do crazy stuff on it. So anyways, when I enter the command "php" it gives me a multiline console but it doesn' do anything when I run a line of php! For instance, I type echo "Hello World" but it just returns it like a typewriter and nothing happens! Can someone please tell me what is going on, and is there a way to exit this?

Firstly check if you have php properly installed:
type in console:
php -v
you should see version of installed php for ex.:
PHP 7.3.3 ...
to run single line of code from console you do it this way:
php -r 'echo "\nHello World\n";'
where \n is new line character.
to enter interactive mode and run multiple lines of code:
php -a
and once you see:
Interactive mode enabled
php >
type:
echo "\nHello World\n";
and hit [Enter] key.
that's it.
to leave interactive mode type:
exit
note lack of ; at the end.
If you want to run from console a php code that you have in a file:
php -f <path-to-the-file>
but this is a default behavior of php so if you miss flag -f and just type:
php
it will do nothing, expecting you providing it a path to file after the php like in the example with flag -f:
php <path-to-file>
So if a programmer intention is to enter an interactive mode but he types only php without any flags, the php will not warn about missing path to the file so programmer may have the impression he's into php interactive mode as he wanted but this is not true.
to see all possible options in php cli, type:
php --help

I believe if you just type php into the terminal it will start a php server process on the local mac. If you want to run a script you have to cd in to to the working directory and type php filename.php.

If you want to use PHP code directly you must enter php -a without a flag you will just enter in php environment.

When running the php command directly from your command line, it behaves as it is reading a .php from the command line
You noticed that it just echo's back any thing you write toward it, this is the same as it is executing a .php file, its echoíng any html back to the browser
Example:
$ php
sss
<?PHP
echo 'Hello!';
?>
More data!
ctrl + d (to signal that the end of the filehas reached)
Output:
sss
Hello!More data!

Related

php from command line empty

I have updated windows today and now when I use the command line and type PHP nothing returns. tried various PHP commands, nothing, not even an error or response. blank.
I had the correct path in the environment variables. Tried to change to another PHP version and even tried removing the path from environment variables but still returns empty!
It doesn't even say that "PHP" is not recognized although I removed the environment variable!!
I have no clue how to solve this.
If you are not passing extra parameters when you call it from command line, that´s how it supposed to work.
Try running:
php --help (to see all options from command line)
Also, to make sure it is running try to do this:
php -v (to check version)
php -F filename.php
The last one should run your php file.
I had the same challenge.
Here's a solution:
Use a standard terminal window (instead of Powershell).
Try php -v again.
IF you get an error mentioning VCRUNTIME140.dll
THEN you need to install run-time components that are required to run C++ applications built using Visual Studio 2015 here: https://www.microsoft.com/en-us/download/details.aspx?id=48145

PHP system() works for some commands, but not all

I have a web server being hosted on a Raspberry Pi B+, running Raspbian. I always have a php "shell" that i can use, but it seems that mine might be messed up somehow. It is an html textarea, with the name="phptorun", and the action file just does eval($_POST['phptorun']);
Since I just have my RPi tucked under a table with no display, I use my phone alot to access the command line.
My question:
When i run something like system("ls"); i get output and the contents of the working directory is displayed. I am working on a C "compiler" (it just uses the command line gcc) but when i do system("gcc");
i get no output at all. i know that the command gcc does put out output, because i have done it before on a different computer.
So why is system("gcc"); not working?
And if gcc isnt installed, wouldnt i get output, just an error?
You need to get more information, it's possible that gcc outputs something to the STDERR for example, which you're missing when you use the system function.
Better try to use the exec function:
exec("gcc 2>&1", $output, $return_code);
Explanation:
gcc 2>&1 redirects STDERR output to the STDOUT
STDOUT is captured into the $output variable
command return code is in the variable $return_code

How to run php code in gedit using External Tools plugin?

I've tried this code in External Tools with actual document as input. But it doesn't work.
#!/bin/sh
/usr/bin/php -r
Syntax check works as expected
#!/bin/sh
/usr/bin/php -l
Outputs either error message or "No syntax errors detected"
For the external tools, set the "Input" to "Current document" and the command is just php (no options):
php
The output in the bottom pane will be the output of your script. Just keep in mind that this is only going to work for local files.
Edit: screenshots...
You can also one of the many Gedit keywords.
$$GEDIT_CURRENT_DOCUMENT_PATH
$GEDIT_CURRENT_DOCUMENT_URI

PHP in command line

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

Tab autocompletion in bash using php

I'm writing a simple script to autocomplete when I press TAB.
The php script contains a simple "echo".
In this case, the autocomplete works but a "tab" is appended to the output
making it useless
Code from the script
scriptPath='/home/hassen/workspace/scripts/bin/test.php'
_dda()
{
local cur
COMPREPLY=()
unset COMP_WORDS[0] #remove "j" from the array
cur=${COMP_WORDS[*]}
IFS=$'\n\n' read -d '' -a COMPREPLY < <($scriptPath --completion "$cur")
return 0
}
complete -F _dda dda
alias dda=$scriptPath
Code from php script
<?php
echo "hello";
?>
Here is the annoying part:
If I print the echo in Python or Ruby, it works like a charm -- ie each time I press TAB, it calls the scripts and output hello.
Is this a bug with PHP or my code?
They seem to disagree at http://bugs.php.net/bug.php?id=52755
It works as desired here, are you very sure the PHP file itself doesn't hold a tab, possibly after the ?>?
Versions: PHP 5.3.2, GNU bash version 4.1.5
CLIFramework provides a command helps you generate the bash completion script by your command definitions, so you don't have to write the completion script by hands:
https://github.com/c9s/CLIFramework
The screencast:
p.s. it also works for zsh
There is a known problem in PHP that is documented here that prevents this from working.
https://bugs.php.net/bug.php?id=53040
Use /usr/bin/php-cgi instead of /usr/bin/php for running the script and it should work.
I had the same issue, not directly with a custom bash completion, but through Makefile bash completion.
Workaround at Makefile bash autocompletion issue with PHP generated targets

Categories