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

Intro to PHP: String Operators & Constants

String Operators are used to modify strings.

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



Watch the video for this lesson:

PHP: String Operators and Quotes



Set up your own LAMP stack programming environment:

YouTube Playlist



Written Lessons: LAMP Tutorials


Assignment Operators

            
              // Define variable
              $gameDev = "Scott";
              // Add to the variable
              $gameDev .= " Cawthon";
              // Print the variable
              echo $gameDev;

$gameDev = "Scott";
$gameDev .= " Cawthon";
echo $gameDev;
Output: Scott Cawthon

Concatenation


  // Define $character
  $character = "Character: ";
  // Define $chosenCar
  $chosenChar = $Character . "Freddy";
  // Print the message
  echo $chosenChar;

$character = "Character: "; $character is a variable containing a string value. The value of $character is "Character: "
$chosenChar = $character. "Freddy"; $chosenChar is a string variable. It's value is equal to $CHARACTER plus "Freddy"
echo $chosenChar; This will print the value of $chosenCar
Output: Character: Freddy

Predefind Constant: .PHP_EOL

PHP_EOL will insert a line break


// Define variables
 $numStars = "5"; 
$gameTitle = "FNAF World"; // Print echo $gameTitle.PHP_EOL; echo "gets $numStars stars!";

$numStars = "5"; $numStars is a variable containing an integer value. It is intialized at 5.
$gameTitle = "FNAF World"; $gameTitle is a string variable intialized at "FNAF World"
echo $gameTitle.PHP_EOL; will print the value of $gameTitle and insert a line break.
echo "gets $numStars stars!"; will print "gets", the value of $numStars, then "stars!"
Output: FNAF World
gets 5 stars!

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

Coding Commanders Storyline:

Coding Commanders Story

Watch the Coding Commanders LAMP Animation:

Coding Commanders: Episode 1



Follow Coding Commanders on:

YouTube

GitHub

Facebook

Instagram

Twitter

Applied Assignment


Background:
You live on post apocalyptic Earth. Aliens called "Bugs" have taken over the planet. As a resistance figher, and web developer, your job is to code warning messages.
Assignment:
Read each description. Use PHP to print the appropriate warning message.
Variables used for messages:



   // Number of bugs in the settlement
   $bugsDetected = 0;
   // Number of shots fired
   $shotsFired = 0;
   // Number of bombs detected
   $bombsDetected = 0;
   // string variable containing any weather alters
   $weather = "";
 

Example:
There are currently 5 bugs detected in the settlement. No shots have been fired. One bomb is detected.


   // Define variables
   $bugsDetected = 5;
   $shotsFired = 0;
   $bombsDetected = 1;

   // Create Message
   $alert = "There have been $bugsDetected bugs detected in our settlement. ";
   $alert .= "$shotsFired shots have been fired. ";
   $alert .= "$bombsDetected bombs have been detected in our settlement.";

  // Print Message
   echo $alert;
  

Message Descriptions:
1) There are 10 bugs detected in the settlement. No shots have been fired. Two bombs are detected. There is a 90% chance of severe thunder storms.
2) There are 10 bugs dectected. 5 shots fired. 2 bombs detected.
3) There are no bugs, shots or bombs detected. There is a hurricane warning. This category 3 hurricane is projected to hit land tomorrow at 3pm.

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