Home » Tutorials » PHP » String Variable

String Variable

Onto the most flexible variable in PHP, the string variable. You might notice how I keep referring to strings as variables. However, strings are much more like objects with methods and properties. Only the string object’s properties is what you might think of as the string. However, I won’t discuss objects much in the early tutorials because they are a bit more complicated. You can manipulate a string in almost every way imaginable in PHP. Previously you already learned how to create a string variable by putting the actual string in quotes following the variable name.

Example
<?php
    $myVar = "Champions";
    echo $myVar;
?>
Result Champions

Nothing special here. We are just echoing a string variable just like before. What if we want to join strings with other strings or strings with variables?

Joining Strings

The concatenation operator in PHP is a little bit odd in comparison. The . joins everything together. Weird choice isn’t it? I thought so.

Example
<?php
    $myVar = "Champions";
    echo "We are the " . $myVar . " of the world.";
?>
Result We are the Champions of the world.

Yes, I was rocking out to that song while writing this. You should too! We have the string “We are the “ that we join to $myVar and again to ” of the world”. That is truly all we have for the simple side of strings. Check out the PHP Strings tutorial to get into more advanced string functions.



Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "String Variable". After Hours Programming. Accessed on October 25, 2024. https://www.afterhoursprogramming.com/tutorial/php/string-variable/.

  • Stewart, Suzy. "String Variable". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/php/string-variable/. Accessed 25 October, 2024.

  • Stewart, Suzy. String Variable. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/php/string-variable/.



0 thoughts on “String Variable”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.