As a new person to PHP I am finding myself asking a lot of questions about it. I receive different newsletters from tech sites, and a few of them have said that there are going to be differences between v4 and v5.

In an E-Book the code below doesnt seem to work properly in v5 and I am assuming that the book was written during the v4 era. This code does have the PHP tags but I have left the tags out for viewing purposes.

echo "Hello ", “world”, “
”; //Don’t use parens
echo (“It’s such a perfect day!

”); // Parens okay
print “Hello to world again!
” ; //Don’t need parens
print (“It’s such a perfect day!
”) ; //Parens okay
printf(“Meet %s%:Age 5d%:Salary $10.2fn”, “John”, 40, 55000);

Now I understand that with echo and print in the same tags, the print overrides echo and therefore echo will not display. However, in the last line of code it doesnt display properly.

Hello to world again!
It’s such a perfect day!
Meet John Age 5d Salary $10.2f

I am assuming that it should display as:

Hello to world again!
It’s such a perfect day!
Meet John Age 40 Salary $55000

I have manually edited this here to acheive the correct display values.

Is the display error a result of the changes between the versions, or is there an error in the actual code??

Not sure why the echo commands don’t display, especially the last one (the first one might defect against having commas as concatenators, rather than periods).

printf(“Meet %s%:Age 5d%:Salary $10.2fn”, “John”, 40, 55000);

In this command 5d displays as normal because it is part of the string. The % by itself isn’t a valid argument for printf() and is probably ignored. $10.2f displays exactly as it is because the $ is escaped, rendering it to be part of the string, rather than as a parsable PHP variable. Try the following:

printf(“Meet %s:Age %5d:Salary %10.2fn”, “John”, 40, 55000);

(not sure if that works though)

Sponsor our Newsletter | Privacy Policy | Terms of Service