PHP VARIABLES AND CONDITIONAL STATEMENT

 

What is a PHP Variable

PHP variable is a container or box that is used to store data, a variable name should be a descriptive name like firstName, phoneNumber, emailAddress, author etc. Avoid using short names example, a for age or h for height. Apart from you no one else knows what you mean by a and h and to make someone understand what those short names stand for you start adding comments to your code. In PHP variable names begins with a dollar sign ($).

Example 1: Storing name, age and phone number of a customer within a variable then render the result on web browser.

<?php

    $name="Joseph Onyeke Ameh";

    $age=31;

    $phoneNumber="08131294018"

    echo $name;

    echo #age;

    echo $phoneNumber;


Output

Example 2: Writing a short story including name and age of the character then replace the name and age with a variable.

   <?php

    echo "There was a man called Musty, Musty is 70 years old, Musty loves being Musty but hate being     70 because 70 comes with gray hair";

    $name="Joseph O Ameh";

    $age="31";

    echo "<br>";

    echo "There was a man called $name, $name is $age years old, $name loves being $name but hate                being  $age because $age comes with gray hair";

?>

If we use a variable we can change all related information at once by simply changing it from the variable name and the change will be update across all the other part of the stories. Try changing the values assign to the variable name to  your name and the age to your age then view it from your web browser. 

Example 3: Write a PHP script that will Sum two values and outputting the result


Difference Between Single Quote and Double Quote in PHP

With a double quote you can embed variables into your string while that can not be done using single quote. This simple means if you write your variable name within your a double quote, it will display the value of the variable as an output but if we use a single quote it will display the variable as it is writing within the code (the variable name is converted to a text within a single quote).

Example: Script showing how to make use of single and double quote.


Concatenating String in PHP

Concatenation is the act of joining two or more strings together. Most programming languages uses the + (Plus) sign to concatenate string but PHP uses . (Period) to perform same task. In most cases the strings are stored within a variable so programmers end up joining the variables together.

Example














Ameh Joseph Onyeke

Hello and welcome to my blog! My name is Ameh Joseph Onyeke a passionate educator and tech enthusiast with a BSc in Computer Science from Benue State University, a PGDE from the University of Maiduguri, and a Master’s in Software Engineering from Bayero University Kano. My journey blends a strong foundation in computing with a love for teaching, aiming to simplify complex tech concepts for learners at all levels. Through this blog, I provide answers and insights across 10+ programming languages to support students and tech enthusiasts in building their skills and confidence. Whether you're solving coding problems or sharpening your programming abilities, this blog is here to guide you. Let’s learn and grow together!

Post a Comment

Previous Post Next Post