Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does PHP print a percent sign when my script is done?

When I run a simple echo 'Hello World'; script in PHP from my terminal on my Mac, I see a percent sign (%) appear at the end of the line.

Why is this? Can I disable this?

~ php -r "echo 'Hello World';"
Hello World%                                          
~ 

Screenshot of issue

Technically, I'm not doing this in bash, I'm running Oh My Zsh, if that matters.

like image 482
romellem Avatar asked Nov 24 '15 18:11

romellem


People also ask

What does percentage sign mean in PHP?

It's the modulus operator, as mentioned, which returns the remainder of a division operation. Examples: 3%5 returns 3, as 3 divided by 5 is 0 with a remainder of 3. 5 % 10 returns 5, for the same reason, 10 goes into 5 zero times with a remainder of 5.

What does percent sign mean in mac terminal?

The shell being used in the current Terminal session will change to the Zsh and the command prompt will change to a percent sign (%). You can return to the bash shell by either quitting Terminal, or at the prompt enter: bash.

Does print work in PHP?

The PHP print StatementThe print statement can be used with or without parentheses: print or print() .

Where does PHP print to?

The docs say that these print to php://output . They are both language constructs, but at a guess, the difference is that print is an expression, but echo is a statement. printf and many friends.


2 Answers

This is from zsh.

Your output doesn't end with a line break. Bash would start the PS1 right after your output, zsh prints a (colored) % and insert a line break itself. You can prevent it by adding a line break yourself

php -r 'echo "Hello World\n";'

Note: I switched " and ', in php '\n' would print it as is but "\n" means line break.

like image 122
clemens321 Avatar answered Oct 14 '22 05:10

clemens321


Add this line to .zshrc :

PROMPT_EOL_MARK=''

It's work for me.

For detail explanation you can go here: https://superuser.com/a/645612

like image 27
iruwl Avatar answered Oct 14 '22 05:10

iruwl