FAQ: PHP Strings and Variables - Using Variables

This community-built FAQ covers the “Using Variables” exercise from the lesson “PHP Strings and Variables”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn PHP

FAQs on the exercise Using Variables

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi, so I took PHP a while back, but a lot of it never really stuck.

I remember something about “$variable = value” versus “$variable == value” and that you should never, never, never confuse the two…but I can’t remember why.

Does anybody have an explanation of this for me?

Thanks!

Assignment versus comparison.

$a = 42
$b = '42'
$a == $b     // true (after type juggling)
$a === $b    // false (not identical)

https://www.php.net/manual/en/language.operators.php

1 Like

Perfect. That makes sense. Thank you!

1 Like

What is the reason for using echo twice? Wouldn’t it be easier and use less code to concatenate it together?

Instead of this:

<?php
// Write your code below:
  
  $name = "Tom Smith";
  $language = "Russian";

  echo "My name is " . $name . " and I have an awesome dog named Mojo" . ".";
  echo "\nMojo speaks " . $language . ".";

Do this:

<?php
// Write your code below:
  
  $name = "Tom Smith";
  $language = "Russian";

  echo "My name is " . $name . " and I have an awesome dog named Mojo" . "." . "\nMojo speaks " . $language . ".";

Thank you

Welcome, @py2490724253,

Good question. Either approach is valid, but you are right to look for ways to simplify the code and reduce repetition.

Something to know about double quoted strings in PHP… They support interpolation, so concatenation is not necessary. The same cannot be said for single quoted strings. They do not support interpolation.

echo "My name is $name and I have an awesome dog named Mojo.\nMojo speaks $language.";

On a technical note, echo is rather a complex process for the server to handle and can use a lot of resources. Composing an entire document into one large string is permitted. We can output it (write it to the request document) with a single echo statement.

Thanks! And thanks for the tech note on echo. That helps. After I went to the next lesson I saw what you are describing.

1 Like