#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31

    How do you connect strings and variables together??


    I am working with Wordpress and I am trying to do a redirect.

    It can accept a string in a function I am trying to use it for: wp_redirect('?page_id=9'); exit();

    But the 9 part, I am retrieving from the database as a variable called $pageNo.

    I have tried the following but to no avail!

    wp_redirect('?page_id=$pageNo'); exit();

    echo 'wp_redirect(\'?page_id='.$pageNo.'\'); exit();';

    $myPage = "'?page_id=".$pageNo."'";
    wp_redirect($myPage); exit();

    I have tried other combinations as well.

    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.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    I'm not familiar with Wordpress but in PHP in general substitution is not done within single quotes.
    PHP Code:
    wp_redirect("?page_id=$pageNo"); exit(); 
    P.S. Note the use of [ PHP ] tags. See the sticky at the top of this forum.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    294
    Rep Power
    8
    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.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    Of course there is not a "best way", but I prefer doing so:

    "bla bla" . (string)$myNumber

    because usually newbies don't really understand what happens if we use a shorter syntax.

    Comments on this post

    • Nanomech agrees : I like that also, is probably the best way actually! ty
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    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.
    HEY! YOU! Read the New User Guide and Forum Rules

    "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

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Location
    Italy
    Posts
    36
    Rep Power
    2
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2006
    Posts
    660
    Rep Power
    31
    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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    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.
    PHP Code:
    echo "<div id=\"divid\" name=\"divid\" align=\"center\">"
    vs
    PHP Code:
    echo '<div id="divid" name="divid" align="center">' 
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  16. #9
  17. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    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.
    HEY! YOU! Read the New User Guide and Forum Rules

    "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

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529
    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.

    PHP Code:
    <?php
        $time0 
    microtime(true);
        
    $abc="hello";
        for (
    $i 1$i <= 100000$i++)
        {
            
    $a=$i;
            
    //echo ('123' . $abc);  //Method 1
            //echo("123{$abc}");    //Method 2
            
    echo '123'$abc;       //Method 3
        
    }
        
    $time1 microtime(true);
        echo(
    '<br>'.($time1 $time0));

    ?>
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,232
    Rep Power
    593
    Using curly braces is pretty rare. Off the top of my head I can't think of a situation where they are necessary.

    Apologies for hijacking this thread.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    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.
    HEY! YOU! Read the New User Guide and Forum Rules

    "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

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Sep 2006
    Posts
    1,799
    Rep Power
    529
    This has been a good post for me. Thanks rePete for starting it!
  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    294
    Rep Power
    8
    Agreed I've learnt a couple of things about strings since reading this thread.

    Although I never escape values with backslashes. Let's say I want to print an image, I'd write:
    PHP Code:
    <?php

    $img 
    "phpwp.jpg";

    echo 
    "<img src='{$img}' alt='PHP logo' />";

    ?>
    Regards,

    NM.
  28. #15
  29. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,692
    Rep Power
    6351
    yeah, but what if you wanted to print an image with the alt text:
    visit our "About us" page
    You would have to escape one or the other.

    Comments on this post

    • Nanomech agrees : I see your point, I've never printed a string which needed to be quoted. I'll play with this now!
    HEY! YOU! Read the New User Guide and Forum Rules

    "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

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo