In this part, we are going to discuss Variables and String in PHP. Variables are the important part of every programming language. If you don’t know about variables then let me explain.

PHP for beginners Part 6, PHP comments

A variable is the symbolic representation of a value. We can say it is a symbol that refers to something. A variable can point to different values. It is clear from its name that the value of variable varies with time. 

In PHP, there are some rules for representing the variable.

Rules For Representing the variables

  • In PHP a variable name always starts with $ sign i.e. $var.
  • It is followed by a letter or underscores i.e. $var or $_var.
  • A variable can contain letters, underscore, dashes, or numbers.
  • Spaces are not allowed in variables.
  • Variables are case sensitive i.e. $var and $Var are not same.
  • Avoid using hyphen sign in variables, it can create confusion i.e. avoid using $the-variable. The PHP can consider it as minus sign.

PHP have some reserved words that have special meaning to PHP. Avoid using these reserved words. Here is the link to the list of these reserved words.

 

Let’s write a simple program to understand the practical working of variables in PHP.

<?php

 $var=20;

 echo $var;

?>

The output of the program is:

 

variables in php

The value will be stored in a variable we have declared as “var”. Whenever we call this variable it will output the value stored in it.

Here is another example;

<?php

 $var=20;

 echo $Var;

?>

Output: 

In this case, browser will display an error. As I have mentioned above variables are case sensitive.

Here is the end of this tutorial if you guys have any question let me know in comment section below.

 

String in PHP

Strings are the common feature in many programming languages. The string is a set of character and it’s defined under double or single quotation mark. The set of character can be a letter, symbol, or number. In programming, term string is also considered as text.

Let’s write a simple program.

<?php

 $var1=”Hello”;

 $var2=”World”;

$var3=$var1.” “.$var2;

echo $var3;

?>

 

The output of the program is:

string in php

 

The dot “.” sign is called concatenation operator in PHP. It combines two elements in PHP.

We can further evaluate this program to add more element using concatenation operator within our string.

<?php

 $var1=”Hello”;

 $var2=”World”;

$var3=$var1.” “.$var2;

echo “{$var3} Again”;

?>

Output: 

string in php

Diffrence between single quote and double quote

Single quotes:
The easiest way to specify a string is to enclose it in single quotes. The single quote is usually faster, and everything quoted within treated as the plain string.
This method is used when we want the string to be written exactly as it is.
When the string is defined in single quotes PHP will not judge it or interpret escape characters excluding single quote with the backslash (‘) and backslash(\) which has to be escaped.
echo ‘This is \’test\’ string’;
//Output: This is ‘test’ string

Double quote:

It will illustrate a host of escaped characters (including some regexes), and variables in the strings will be evaluated.
An essential point here is that you can use curly braces to separate the name of the variable you want to be evaluated.
For example, let’s say you have the variable $type and you what to echo “The $types are” That will look for the variable $types. To get around this use echo “The {$type}s are” You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
Practice double quotes in PHP to avoid having to use the period to depart code in the string.
$count = 1;
echo “The count is $count”;
//Output: The count is 1

 

 

LEAVE A REPLY

Please enter your comment!
Please enter your name here