#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    402
    Rep Power
    7

    Question The Concatenation Puzzle


    Php Experts,

    I have a concatenation question.
    I have seen tutorials teach concatenation in these 2 formats but the last format the most.
    Last format puzzling to me.
    Lessons teach to just add the dot to join 2 strings or variables (if I understood that correct).

    PHP Code:
    <?php
    $full_name 
    $first_name." ".$surname;
    echo 
    "1. $full_name";
    ?>
    Based on "to add a dot to join 2" is a good example above. Note the space inbetween the 2 double quotes. This space separates the first & surname.
    So far, so good. So logical.

    PHP Code:
    $full_name "".$first_name." ".$surname."";
    echo 
    "2. $full_name";
    ?>

    But wait! What is this example just above ? Weird!
    I see a dot prior to $first name and I see a dot after the $first name and likewise I see in the $surname.
    Why the extra dots (concatenators) prior and after the variables (or strings) ?
    Ok, I can't understand the dot prior the $first_name. But, I can understand the dot after the $first_name as that dot joins the space inbetween the 2 double quotes.
    And, I can understand the dot that comes after the 2 double quotes (that represent the space) and prior the $surname. But, I can't understand the dot that comes after the $surname.
    I'm highlighting the 22 dots I don't understand. Just what exactly are they doing and what are they for ? What do they represent ?

    Anyway, which format you use, the 1st or the last ? Somebody amended my script many months back to so:

    Why I See Blank Page After Submitting Form And Why Echoing Fails ?

    PHP Code:
    $body  ".$first_name." ".$surname.",
                        <
    html>
                        <
    head>
                        <
    title>Activation Link</title>
                        </
    head>
                        <
    body>
                        
    You need to click on the following link <a href=".$account_activation_link.">.$account_activation_link.</ato activate your account.
                        </
    body>
                        </
    html>"; 
    I'm switching it to this:

    PHP Code:
    $body  "$first_name." ".$surname,
                        <html>
                        <head>
                        <title>Activation Link</title>
                        </head>
                        <body>
                        You need to click on the following link <a href="
    .$account_activation_link.">.$account_activation_link.</a> to activate your account.
                        </body>
                        </html>"


    Good idea ?
    Last edited by UniqueIdeaMan; December 16th, 2017 at 10:01 AM.
  2. #2
  3. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,531
    Rep Power
    111
    Sorry for a quick answer, but just saw your question omw out the door...

    Just as a side question, are you aware the difference between using a single quote or a double?

    You do no need to surround your variables within quotes, such as your $first_name above. To simply add a space between 2 variables, it can be similar to the way you are doing so.
    PHP Code:
    $first_name ' ' $last_name 
    If you've used JavaScript or such, a period is similar to a plus sign. (object + object + object) An item within single quotes is an object. A variable is an object. The period is a joiner of the 2 objects. The example I've provided has 3 objects. A PHP variable ($first_name), a space, and another PHP variable ($last_name). The 2 periods join each object.
    One way to ignore the period, which many frown upon including myself, is using variables within double quotes. If using double quotes, you may need to escape many special characters.
    PHP Code:
    "My name is $name and I am here." 
    If using single quotes, you cannot do this in the same manner. It must be:
    PHP Code:
    'My name is ' $name ' and I am here.' 
    Maybe think of the quotes of an inside/outside. When within quotes, you are in HTML and plain text. When outside the quotes, you are in the world of PHP.

    Comments on this post

    • UniqueIdeaMan agrees : Guy solved 2 of my thread isssues froma single thread and a single post at that!
    Last edited by Triple_Nothing; December 16th, 2017 at 10:51 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  4. #3
  5. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,531
    Rep Power
    111
    Just to go over your final attempt/example/question...
    PHP Code:
    $first_name 'Triple';
    $surname 'Nothing';
    $body1  = ".$first_name." ".$surname.", <html>...
    echo 
    $body1;

    $body2  = "$first_name." ".$surname, <html>...
    echo 
    $body2
    $body1 will output .Triple..Nothing. and should provide an error. The echo will produce the visible periods because they are within the quotes, and to be treated as nothing more than a period on the end of a sentence. It will then error because after the quotes following the $surname., you are providing HTML in the world of PHP, since that code is not within the quotes.

    $body2 will output Triple..Nothing, <html>... and should continue clean. Obviously the actual code isn't what you want, but this is just an example.

    There are instances where double quotes may be a bit more of a must, but I need to ask you to try and never use them for now. And as time goes by, find those occasional "here and there" cases where you may come to need them for particular reasons, and not for casual use.

    And if you are truly intending on placing a name or such coding prior to your opening HTML tag, there is even more wrong here...

    Edit: And since I never really defined what you were really asking or not understanding about the dot before $first_name or after $surname... We can't answer too much, since the example you are referencing may have had its own point. The reason it is before and after those 2 variables is the exact same reason in relation to the space. There are a set of quotes provided to house something. In your provided example, these quotes are blank, so useless, but nonetheless they still exist, so the dot is joining the blank object to the beginning and end.

    Edit2: The below code will also be invalid:
    PHP Code:
    <a href=" . $account_activation_link . "
    Output:
    Code:
    <a href=http://www.google.com/>
    If using quotes correctly, you've 2 options:
    PHP Code:
    // Double Quotes (Need to escape a quote via backslash to preserve it)
    $url "<a href=\"" $account_activation_link "\">";
    // Single Quotes (No need to escape most any special characters)
    $url '<a href="' $account_activation_link '">'

    Comments on this post

    • UniqueIdeaMan agrees
    Last edited by Triple_Nothing; December 17th, 2017 at 12:06 AM.
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    402
    Rep Power
    7
    Originally Posted by Triple_Nothing
    Sorry for a quick answer, but just saw your question omw out the door...

    Just as a side question, are you aware the difference between using a single quote or a double?

    You do no need to surround your variables within quotes, such as your $first_name above. To simply add a space between 2 variables, it can be similar to the way you are doing so.
    PHP Code:
    $first_name ' ' $last_name 
    If you've used JavaScript or such, a period is similar to a plus sign. (object + object + object) An item within single quotes is an object. A variable is an object. The period is a joiner of the 2 objects. The example I've provided has 3 objects. A PHP variable ($first_name), a space, and another PHP variable ($last_name). The 2 periods join each object.
    One way to ignore the period, which many frown upon including myself, is using variables within double quotes. If using double quotes, you may need to escape many special characters.
    PHP Code:
    "My name is $name and I am here." 
    If using single quotes, you cannot do this in the same manner. It must be:
    PHP Code:
    'My name is ' $name ' and I am here.' 
    Maybe think of the quotes of an inside/outside. When within quotes, you are in HTML and plain text. When outside the quotes, you are in the world of PHP.
    Don't apologise man! You helped a lot from your very 1stt reply post. I am giving you REP for this as you helped me solve the case. I still got to read your next post but I'm sure it would teach me more!
    Look what I mentioned about you:
    Syntax error, unexpected "

    Anyway, I do know that you use sngl quotes to mention a string and whatever you say in that string gets shown as is.
    Whereas you use dbl quotes to define a variable (I know dbl quote not needed).
    You know what, made notes half or a yr back regarding sngl quotes, dbl quotes, strings, etc. so you can see what I know or not. Bear in mind I'm not young aged and you can't learn things at this age or if you learn it then you can't remember it much. And so, I can't always remember the things in my notes.
    Saying all this, I did not know the things you taught me here (concatenator not needed inside dbl quotes)! Or, if these things exist in my notes then I forgot about them as I have not gone through my notes for nearly 6 mnths now.
    Last edited by UniqueIdeaMan; December 17th, 2017 at 01:14 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    402
    Rep Power
    7
    Originally Posted by Triple_Nothing
    Just to go over your final attempt/example/question...
    PHP Code:
    $first_name 'Triple';
    $surname 'Nothing';
    $body1  = ".$first_name." ".$surname.", <html>...
    echo 
    $body1;

    $body2  = "$first_name." ".$surname, <html>...
    echo 
    $body2
    $body1 will output .Triple..Nothing. and should provide an error. The echo will produce the visible periods because they are within the quotes, and to be treated as nothing more than a period on the end of a sentence. It will then error because after the quotes following the $surname., you are providing HTML in the world of PHP, since that code is not within the quotes.

    $body2 will output Triple..Nothing, <html>... and should continue clean. Obviously the actual code isn't what you want, but this is just an example.

    There are instances where double quotes may be a bit more of a must, but I need to ask you to try and never use them for now. And as time goes by, find those occasional "here and there" cases where you may come to need them for particular reasons, and not for casual use.

    And if you are truly intending on placing a name or such coding prior to your opening HTML tag, there is even more wrong here...

    Edit: And since I never really defined what you were really asking or not understanding about the dot before $first_name or after $surname... We can't answer too much, since the example you are referencing may have had its own point. The reason it is before and after those 2 variables is the exact same reason in relation to the space. There are a set of quotes provided to house something. In your provided example, these quotes are blank, so useless, but nonetheless they still exist, so the dot is joining the blank object to the beginning and end.

    Edit2: The below code will also be invalid:
    PHP Code:
    <a href=" . $account_activation_link . "
    Output:
    Code:
    <a href=http://www.google.com/>
    If using quotes correctly, you've 2 options:
    PHP Code:
    // Double Quotes (Need to escape a quote via backslash to preserve it)
    $url "<a href=\"" $account_activation_link "\">";
    // Single Quotes (No need to escape most any special characters)
    $url '<a href="' $account_activation_link '">'
    Thank you! I have a question regarding your final code (sngle quotes) but I will ask tomorrow after a series of tests. (Way past bed-time now!).
    As of now, look at the test below that I did to learn things before opening this thread or before you gave me your replies.
    And so, yes, I do experiment and learn things by myself but sad thing is I quickly forget what I learnt unless I'm constantly practicing every now and then and it has unk into my subconscious. Subconscious is very powerful. More than the conscious.

    My Last Night's Experiment & Findings:

    PHP Code:
    <?php

    $first_name 
    "Russel";
    $surname "Daniels";


    <?
    php
    //Pass
    $full_name $first_name." ".$surname;
    echo 
    "1. $full_name";
    ?>
    <br>

    //Pass
    $full_name = "".$first_name." ".$surname."";
    echo "2. $full_name";
    ?>
    <br>


    <?php
    //Pass with spaces at the beginning & at the end of the sentence
    $full_name "| ".$first_name." ".$surname." |";
    echo 
    "3. $full_name";
    ?>
    <br>


    <?php
    //Fail
    $complete_name "| .$first_name. .$surname. |";
    echo 
    "4. $complete_name";
    ?>
    <br>


    <?php
    //Fail
    $complete_name "| .$first_name .$surname. |";
    echo 
    "5. $complete_name";
    ?>
    <br>


    <?php
    //Fail
    $all_name "| $first_name$surname. |";
    echo 
    "6. $all_name";
    ?>
    Maybe the experiment has a lot of bad practice. But hey, you won't learn how to kick hatrick goals from a mile away like Pele unless you trip over the ball a few times!

    You stay tuned! Look out for my upcoming threads. Watch out you might find me asking a few questions and think: "He should have known that as it's obvious" or "how could he forget ?"". But, just don't loose your cool (like Requinix and a couple or few others here and there).
    Last edited by UniqueIdeaMan; December 18th, 2017 at 07:50 AM.
  10. #6
  11. No Profile Picture
    Super Moderator
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2009
    Location
    Hartford, WI
    Posts
    1,531
    Rep Power
    111
    Just kinda curious as to how you define something as a "Fail"... The examples you have listed should all be just fine. Unless I am overlooking something, the below should output as defined...
    PHP Code:
    $first_name = "Russel"
    $surname = "Daniels"

    $full_name = $first_name." ".$surname
    echo "1. $full_name";
    // Output: 1. Russel Daniels 

    $full_name = "".$first_name." ".$surname.""
    echo "2. $full_name";
    // Output: 2. Russel Daniels

    $full_name = "| ".$first_name." ".$surname." |"
    echo "3. $full_name";
    // Output: 3. | Russel Daniels | 

    $complete_name = "| .$first_name. .$surname. |"
    echo "4. $complete_name";
    // Output: 4. | .Russel. .Daniels. | 

    $complete_name = "| .$first_name .$surname. |"
    echo "5. $complete_name";
    // Output: 5. | .Russel .Daniels. | 

    $all_name = "| $first_name$surname. |"
    echo "6. $all_name";
    // Output: 6. | Russel. Daniels. | 
    He who knows not that he knows not is a fool, ignore him. He who knows that he knows not is ignorant, teach him. He who knows not that he knows is asleep, awaken him. He who knows that he knows is a leader, follow him.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2017
    Posts
    402
    Rep Power
    7
    Originally Posted by Triple_Nothing
    Just kinda curious as to how you define something as a "Fail"... The examples you have listed should all be just fine. Unless I am overlooking something, the below should output as defined...
    PHP Code:
    $first_name = "Russel"
    $surname = "Daniels"

    $full_name = $first_name." ".$surname
    echo "1. $full_name";
    // Output: 1. Russel Daniels 

    $full_name = "".$first_name." ".$surname.""
    echo "2. $full_name";
    // Output: 2. Russel Daniels

    $full_name = "| ".$first_name." ".$surname." |"
    echo "3. $full_name";
    // Output: 3. | Russel Daniels | 

    $complete_name = "| .$first_name. .$surname. |"
    echo "4. $complete_name";
    // Output: 4. | .Russel. .Daniels. | 

    $complete_name = "| .$first_name .$surname. |"
    echo "5. $complete_name";
    // Output: 5. | .Russel .Daniels. | 

    $all_name = "| $first_name$surname. |"
    echo "6. $all_name";
    // Output: 6. | Russel. Daniels. | 
    Some were fail because they echoed the concatenators and some echoed space prior $first_name and some after $surname. And, I never wanted them echoed like that. And so, fail means they failed on my requirement. Not failed on echoing.

    Oops! You are a Super Mod. A Mod afterall. Nevermind. I'm glad a Mod and a Super one at that is responding to me.
    Opening another thread in an hr or 2 on "banned words" filter. Requinix might not like that thread and he has reasons too. He knows what I'm talking about. Just because the issue has been dealt with in one of the other forums he moderates does not mean I can't pickup from there and bring the subject here and continue here where it came to a deadend over there.
    I need peoples' opinions over here.

IMN logo majestic logo threadwatch logo seochat tools logo