When I first took the exam, the strings section was the section that I got my worst score. It took me days to figure out what went wrong. I don’t think that there’s any PHP developer that doesn’t work with strings at least once a day and I do work with strings and patterns pretty much the whole day long. After you get to know strings manipulation a bit more you will see that it is as powerfull as Arrays.
For a start do you know what’s the difference between echo and print? Basically is only the fact that print returns a interger (1 or 1 – true or true always) and prints the string in the document, but you should know how both work, because that is what is going to be asked from you in the exam.
Take a look in the print.
- Is a language constructor so you don’t need the parenthesis
- it will work as a function because it will return a value
- it does not always work in the same way as echo
If you do <?php print 9.9 ?> it will print on the document 9.9, but, if you <?php echo 9.9 ?> it will print on the document 99. This is the first big catch with print and echo.
Now let’s take a look in the echo:
- is a language constructor so you don’t need to use the parenthesis (that you knew) but if you want you can use the parenthesis just like print
- it does not return a result after printing the string in the document
- you can pass more than one string to print with echo using commas (know this by heart)
- dot’s are used to concatenate strings in an echo
- double quotes and single quotes does not work in the same way
Using the same example as before would not make much sense, but I have one that will help you understand what kind of question you will see in the exam when working with strings.
What is the final result that is going to be printed?
echo ’55’ . print(‘0.4’) + 3;
To answer correctly this question you must know how both constructors work, so let’s “debug” this question.
print(‘0.4’) will print 0.4, but echo print(‘0.4′) will print 0.41. Seems odd, but you got to remember that print will first print it’s result and then echo will print the return of the print constructor, so 0.41.
echo ’55’ . print(‘0.4’) will print then 0.4551. Now things get confusing, but it’s not hard. The print constructor will work a bit faster than echo because it only have a single string to print and echo will have 2 arguments to take care of, so print will print first 0.4 then echo will print it’s first argument, 55 and finally it will print it’s last argument, 1 that is the result of print. In the end we will have 0.4551 as result.
Now comes the third part of the question, adding the value 3 to the string. You must noted by now that the whole string is numeric, so PHP converting the string to float and then adding 3 would not be a problem, but how do you think it would work if we added the string ‘ab’ with the ’55’ string and an ‘c’ inside the print? In this case it would try to convert the result of the print first to a float number, so ‘c’ would be excluded, and then add the value 3 and then concatenate the string.
This is all and actually all of this are all my notes about strings when studying for the exam. Here are some links that you should take a look at to learn more about strings: