PHP Basics Part 2-Learn Variables

Sharing is Caring
Share

In the previous post of PHP basics part 1, we have gone through the php definition, details, how to set up a php development php variablesregion and broad level syntax details. In this php basic part 2, we are going to dive further and take a look at some of the most important aspects of php.

If you haven’t read php basics part 1, click here.

It is very important to begin it from the scratch level to learn php, if you are new to it.

Ok, so now coming back to php.

PHP Basics Part 2-Learn Variables

PHP is a loosely typed language and it is extremely easy to learn and implement. In next few parts of this php tutorial, we will cover various important aspects of php. To name few, we will be dealing with;

  1. PHP Variables
  2. PHP Functions
  3. PHP while and for loop
  4. If, Else, Elseif statement in PHP
  5. PHP Array
  6. PHP Strings

And much more.

In this part, we will go through the php variables.

PHP Variables

In simplest term, variables are value to store information. You can use variables to store any kind of information and then use it at a later stage to perform certain actions on it or use it to perform certain actions with your website or application.

Variables in PHP or for that matter, in any programming languages are extremely useful.

You may use variable to store all kind of information for example, usernames, numbers, calculations etc.

So in order to use variables in php, you first create a variable and then assign a value to it. You can assign a value or expression to a variable.

Take a look at these examples,

$x =5;

$y=6;

$a=$x+$y;

$x and $y in the above mentioned examples are values where as $a=$x+$y is an expression.

You can call either $x, $y or $a based upon your requirement.

Now don’t get confused with small equation mentioned above for this example, you can use even larger values and numbers using variable including sentences.

As mentioned earlier, php is a loosely typed language which means that you can assign any name or value to any variable. You just have to follow few rules to name variables.

Following rules should be followed while naming php variables;

A variable name should always start with a dollar sign.

A variable name must start with a letter or the underscore.

A variable name cannot start with a number.

A variable name can only contain alphanumeric characters and underscores (A-z, 0-9, and _ )

Variable names are case sensitive ($hello and $Hello are two different variables).

PHP Variable Examples

<?php

$String = "This is my php variable with names string which represent a sentence in php";

echo "$String";

?>
<?php
$a=23456;
$b=345667;
$C=$a+$b;
echo "$C";
?>

 

<?php

function variabletest () {
	$abc="this is the first variable to used in a function";
	$variablewithinfunction = "abcd1234";
	echo "first variable within function is a variable and it is $abc";
	echo "variable within function and that is $variablewithinfunction";
}
variabletest ();
?>

 

In a live website, these variables are used for lot of purposes like storing custom title tags for various pages or storing information in contact forms.

These variables are used in functions and arrays for performing related activities. As we will move ahead with this php learning lesion, we will cover all of them and that will also contribute to your understanding of variables.

In the meantime, try out different variable names and values. You can use the code and just change the value.

Let me know if you have any questions.

Sharing is Caring
Share
About akhilendra

Hi, I’m Akhilendra and I write about Product management, Business Analysis, Data Science, IT & Web. Join me on Twitter, Facebook & Linkedin

Speak Your Mind

*