Zend Exam Tips: Strings

When you are taking an exam Strings and Patterns are by far one of the hardest part of the exam. I will take a small overview of the strings section in this category of the exam.

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:

About mcloide

Making things simpler, just check: http://www.mcloide.com View all posts by mcloide

3 responses to “Zend Exam Tips: Strings

  • Neil Wilkie

    Hi there, I’m not sure your reasoning behind the print/echo differences is completely correct. as you say, the main differences between them is that print will return a value (1) once it has completed, which allows it to be used as part of another expression, and that echo can take a comma separated list of strings to print. The way in which both cast their input to a string is the same. Your first example isn’t correct, both print and echo will cast the float 9.9 to the string ‘9.9’.

    print 9.9; // ‘9.9’
    echo 9.9; // ‘9.9’

    When picking apart your other example you’ve also got a bit mixed up. Really there are two expressions being run here, an echo and a print. As the echo utilises the result of the print, the print will be outputted first (this has nothing to do with the print being faster than the echo, they both only have a single argument).

    print(’0.4′) + 3; // ‘3.4’

    Here the parenthesis confuse things a little, as print is a language construct they aren’t required. Before print receives its argument we need to perform the addition, which involves casting the string ‘0.4’ to a float, and adding the 3. This result is then cast back to a string by print and outputted. With the print out of the way we can simplify the echo statement by replacing the print part with the resulted result, 1.

    echo ‘55′ . 1; // ‘551’

    Here’s we’re first concatenating the string ’55’ with ‘1’ (cast from an int) and then outputting the result. You can then put the two statements back together to give the end result.

    echo ’55’ . print(‘0.4’) + 3; // ‘3.4551’

    Hope that makes sense! Thanks.

  • mcloide

    Hi Neil thanks for the input.
    Let’s go by steps. Have you tested echo ’55’ . print(‘0.4’) + 3 ?? This is actually a modified version of the real question that I got on the exam. As you know print will always return 1 when it finishes, or not, printing the string, so when you echo print ‘0.4’ it will automatically print 1 with the print statement, therefore you will have 0.41 printed, but echo has the first argument being passed as 55, so it will, after the print returns it’s value, print the 55 and we will have 0.455 and finally printing the 1, having 0.4551. When it ends we have a casting operation, so we will add the 3 on the cast float value of 0.4551 giving the last result.
    About the first set of questions with the difference between echo and print, what you need to realize is the way that the parameters has been passed. On echo I have ‘9’.’9′ = ’99’ and on print I have 9.9, with will print 9.9. If on print it was ‘9’.’9′ the result would be the same as on echo and if I had on echo ‘9.9’ the result would be same as on print and then having the difference only having print returning 1 whenever it actually prints or not on the document.
    Thanks for your input. Keep coming back.

  • Neil Wilkie

    Hi there, your logic still doesn’t make sense. Once print has finished it will have sent its output to STDOUT, after that has happened you cannot alter the value which was outputted. Any other operations which you perform will use the result returned by print (1). Changing your example a little will show that the 3 isn’t added to the outputted string at the end.

    echo ’55’ . print(‘1’) + 3; // ‘4551’

    Here we get the 4 at the start because the ‘1’ and the 3 are added together before being outputted by print. echo then outputs the ’55’ and the 1 returned by print. If this was to follow your logic we would first output ‘1’, then add on the ‘551’, giving us ‘1551’, then finally add the 3, giving ‘1554’, which isn’t right. Another way to see what is going on is to add a marker to the end of the print statement.

    echo ‘55′ . print(’0.4′) + 3 . ‘-‘; // ‘3.4-551’

    Here the dash is outputted on the end of the 3.4, before the ‘551’ is outputted by echo.

    Hope that helps! Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: