Home Linux HTML/CSS PHP React VR T-shirt Shop Blog PHP Fiddle

Intro to PHP: echo

Quick Copy Boxes



Echo is used to print strings. It is actually not a function. It is a language construct. That is why you do not have to use parentheses with it

At the bottom of this page there is a code editor pre-loaded with the lesson's code.

Print Characters

echo 'I love FPS!';

echo 'I love FPS!'; will print 'I love FPS!'
Output:
I love FPS!

Print Variables


  // Declare variables
  $gameTitle = 'Wolfenstein';
  $numStars = 5;
// Print game title echo $gameTitle;
// Insert line break echo "/n";

// Declare variables This is a comment. The line will not execute. It is a way to make notes in your code. It is important to include comments that make your code easy to follow.
$gameTitle = 'Wolfenstein'; $gameTitle is a string variable with a value of 'Wolfenstein'.
$numStars = 5; $numStars is a variable containing the integer 5.
echo $gameTitle; will print the value of $gameTitle
Output: Wolfenstein
echo "/n"; will insert a line break, so the next output prints on a new line.

Concatenation

Putting ' . ' between a string and a variable allows you to print a combination of variables and strings on the same line.



 echo 'I give'  . $gameTitle . ' ' . $numStars . 'stars!';
 echo '/n'; 
      
      

echo 'I give' . $gameTitle . ' ' . $numStars . 'stars!';
Output: I give Wolfenstein 5 stars!
echo '/n'; inserts a line break

Single VS Double Quotes


 echo "I played $gameTitle today."; 
echo 'I played $gameTitle today.';

echo "I played $gameTitle today."; When you use double quotes, variables will be evaluated.
Output: I played Wolfenstein today.
echo 'I played $gameTitle today.'; When you use single quotes, variables will not be evaluated. The literal characters will print.
Output: I played $gameTitle today.

If you find my tutorials helpful, please consider donating to support my free web development resources.

The more money I raise, the more content I can produce.

Thank you,
Commander Candy

DONATE

Check out my blog:

Coding Commanders Blog

Watch the video for this lesson:

PHP: String Operators and Quotes

Applied Assignment


Background:
You are hired to develope a video game librbary application. The application is designed to log the client's video game collection. It will include options to search the client's database of games based on user defined criteria. Options will include search by genre, game developer, number of stars and number of times played.
Assignment:
Right now you are creating the messages that will be returned to the user. Use PHP to print each message. A description of the message will be provided.
Variables used for messages:



   // $gameTitle holds the name of the selected game's title.  Below it is intialized as an empty srting.
   $gameTitle = "";
   // $numStars repersents the number of stars given to each game.  Below it is initialized at 0.
   $numStars = 0;
   // $gameDev repersents the selected game developer.
   $gameDev = "";
   // $timesPlayed repersents the number of times the client has played the game.
   $timesPlayed = 0;
 

Example:
Create a message that tells the client the game title and the name of the game developer:
echo "You have selected " . $gameTitle . " developed by " . $gameDev;
Message Descriptions:
1) Create a message that tells the client the game title and the number of stars it is given.
2) Create a message that tells the client the game title and the game developer.
3) Create a message that prints the selected game title, followed by a colon and a space, then prints the number of stars. The output should look something like this: Wolfenstein: 5 stars .
4) Create a message that prints the selected game title, followed by a colon and a space, then prints the number of times the game has been play. The output should look something like this: Wolfenstein: 10 plays .
5) Create a message that lists the game title, game developer, number of stars and number of times played.

Once you finish the assignment, play around with variable values and your echo statements in the code editor box below.


Back to Previous Lesson | Continue to Next Lesson