How do you connect strings and variables together??
Discuss How do you connect strings and variables together?? in the PHP Development forum on Dev Shed. How do you connect strings and variables together?? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Is this possible. Is there a way to store this whole line '?page_id=$pageNo'
into a variable as a string, including the quotes so that I can just place the variable into wp_redirect($String);
Thanks
Last edited by rePete : January 24th, 2013 at 12:18 PM.
Posts: 196
Time spent in forums: 1 Day 23 h 53 m 4 sec
Reputation Power: 7
I've always been told it is best practice to put any variables within curly braces if placed within a string. How true this is I don't know but I choose this method 99% of the time:
PHP Code:
wp_redirect("?page_id={$pageNo}"); exit();
There are many many ways to do it though.
Hope this helps.
Regards,
NM.
__________________
"WERE NOT WORTHY!"
"WERE NOT WORTHY!"
Posts: 9,811
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
With PHP, there are many "correct" ways of doing things. There are people who will argue that one is faster. They are correct, technically. One is nanoseconds faster than the other, and proving which is an exercise left up to the reader, or to someone who can google the last time I proved it.
The correct ways:
PHP Code:
$a = '123' . $abc;
$a = "123{$abc}";
$a = '123' . strval($abc); //unnecessary
$a = '123' . (string)$abc; //unnecessary
$a = sprintf('123%s', $abc); //the C and C++ way, overly complex
$a = implode(array('123', $abc)); //Just silly, don't do this.
In addition to these ways, there's a special way. echo accepts a comma-delimited list of arguments without needing parentheses:
PHP Code:
echo 'abc', $def, '123';
Weird, I know.
Anyway, the official spec/standard says to use the "123{$abc}" method. So just do that.. Note the double quotes.
The PHP manual page on strings has more information if you're interested.
"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin
"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002
Posts: 36
Time spent in forums: 4 h 37 m 48 sec
Reputation Power: 1
Quote:
Originally Posted by ManiacDan
PHP Code:
echo 'abc', $def, '123';
Weird, I know.
This is the only way that is fast enough to say "this is faster". All other ways are not faster, or at least, the randomness of benchmark results is much much much higher than the performance difference you can measure
The reason why echo $a, $b is faster is obvious and valid in ALL programming languages: concatenating a string can be a string has a cost.
But still, this has some relevance only if you are printing ENORMOUS strings.
Posts: 658
Time spent in forums: 1 Week 1 Day 3 h 47 m 13 sec
Reputation Power: 30
Thanks Guys!
The quote things is a little confusing to me. So, in general, should I learn to use double quotes " when I am referring to a string inside brackets??
eg.
url("http://www.blah.com");
and with echos, I use single quotes??
echo '<p>hello</p>';
Thanks for enlightening me on this. I don't understand why there is not one coding standard?? Why not one language?? Why have php, mySQL, html, etc.
Posts: 2,885
Time spent in forums: 1 Year 2 Weeks 3 Days 8 h 17 m 9 sec
Reputation Power: 581
Not quite. Brackets have nothing to do with it. Strings in general should be using double quotes. Single quotes are used when you have a lot of special characters in the string. Special characters in a double quoted strings need to be escaped (\) but not in single quotes. IMO it is mostly for readability.
Posts: 9,811
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
Single-quoted strings are not examined or interpolated. If you put:
'123 {$abc} \t'
That is literally what you will get. If, however, you changed those to double quotes, {$abc} will be replaced with the value of variable $abc, and \t will be replaced with a tab.
As for escaping, you only need to escape characters which match the surrounding quotes. If you don't want to end your string, escape it:
'It\'s as easy as 123!';
"Then my mom said \"Clean your room!\"";
Characters still need to be escaped in both quoting types, specifically the quotes and backslashes.
Posts: 1,464
Time spent in forums: 2 Weeks 1 Day 6 h 21 m 36 sec
Reputation Power: 526
I either never knew or forgot what the curly brackets were used for in strings. I typically only use single quotes. Funny that I haven't come across the curly quotes in someone else's code and been forced to figure out what they meant. Do many of you use them? Regardless, to speed, they are effectively all the same.
Posts: 9,811
Time spent in forums: 2 Months 3 Weeks 19 h 13 m 52 sec
Reputation Power: 6112
The coding standard says you should always include the curly braces. They're necessary for any time a variable isn't followed by a space, as in:
echo "The history of the {$century}s";
They also help code highlighters highlight properly, as most won't properly handle:
echo "The first element is $arr[0]";
PHP will handle more sloppy coding styles well, that's why it's designed the way it is. The "official" recommendation is to use the "complex syntax" always, though the "simple syntax" (without the braces) is alright for very simple scalars and object properties. The manual entry on string parsing discusses it more in depth, and provides examples of how you can be easily bitten by using the "simple" syntax.