View Full Version : whats the ' for
opeboyal
07-28-2009, 09:23 PM
<?php
$money = 30;
$pet = "Kitten";
$new = sprintf("It costs $%03.2f for a %s.\n",$money,$pet);
$new2 = sprintf("%'.-20s%3.2f",$pet,$money);
echo $new;
echo $new2;
?>
I just don't understand what the ' is for. When you remove it, it treats the string literally. why is there only one though?
JasonDFR
07-29-2009, 10:09 AM
"%'.-20s%3.2f"
If you are talking about the single quote above, the entire string is not formed correctly, thus, I don't think there is any good reason there is only one.
In the above, a string is starting with the first double quote and ending with the next, then restarting with the next double quote and ending again with the double quote after the f, in affect causing the word RED to not be included in the string at all.
Also you said that removing the ' will treat the string literally. I don't believe this is the case. Using single quotes to enclose a string will cause the string to be treated literally. Using double quotes will cause variables and placeholders to be evaluated.
If you are thinking something else, or I missed something, let me know.
I would agree that the single single-quote appears to be a typo and should be removed. It also seems that the double-quotes around the word RED need to be escaped, so "RED" is counted as part of your string, like so:
<?php
$new2 = sprintf("%.-20s%3.2f",$pet,$money);
?>
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.