Variables are used to store information which can be referenced and manipulated within a computer program. These variables also play a vital role in labeling data with a descriptive name, which makes it easier for the users to understand the program clearly and impressively. Some programmers also consider the variables as containers which can hold information.
In every program, the sole purpose of a variable is to label the data and store it, somewhere in the memory. This data can be used throughout the program, anytime sooner or later.
When it comes to PHP, declaring and storing variables are different. The variables in PHP hold the value or information like string, integar etc. In PHP, a variable is declared by using the “$” sign. It should be followed by the name of the variable.
While declaring variables in PHP, you might have to follow few basic rules, including the following:
For example: $name=”hello world”;
Here $name is a variable and “hello world” value.
Example of how to store string, integer, and float values in PHP variable:
$string=”hello I am a string variable”; //this is a php string value
$integer=600; // this is a php integer value
$float=50.8; // this is a php float value
echo “this is a string value: $string<br>”;
echo “this is a integer value: $integer<br>”;
echo “this is a float value: $float<br>”;
this is a string value :hello I am a string variable
this is a integer value: 600
this is a float value: 50.8
Addition of two number-
Echo “sum of two number is: $result”;
Sum of two number is: 500
Table of Contents
The scope of a variable can be defined as its extent in program. It can be accessed in the program. Easily explained, it is the portion of a program within which it is clearly visible and accessible. PHP has three variable scopes:
These are the variables which are declared within a function. It is so named as it has scope only within that particular function. It is not able to access outside the particular function. Any declaration of a variable that you use outside the function with the similar name, as the one used within the function, becomes a completely different one.
$num = 60;
//This $num is local to this function
//the variable $num outside this function
//is a completely different variable
$num = 50;
echo “local num = $num \n”;
//$num outside function local_var() is a
// completely different variable than that of
// inside local_var()
echo “Variable num outside local_var() is $num \n”;
Local num = 50
Variable num outside local_var() is 60
The variables that are declared outside a particular function are known as global variables. One can access directly outside that particular function. In order to access the variable within a function, one need to use the word “global”, in front of the variable to refer to the particular global variable.
$num = 20;
// functionto demonstrate use of global variable
//we have to use global keyword before
// the variable $num to access within
// the function
echo “variable num inside function : $num \n”;
echo “Variable num outside function : $num \n”;
Variable num inside function : 20
Variable num outside function : 20
It is the characteristic of PHP to delete a variable. Once the variable completes its execution and the entire memory is freed, it is to be deleted. But sometimes, the variables need to be stored even after the function is executed. To conduct this, PHP developers use static keyword and the variables then are known as the static variables.
// function to demonstrate static variables
// static variable
static $num = 5;
$sum = 2;
echo $num, “\n”;
echo $sum, “\n”;
// first function call
// second function call
Learn more about PHP variables and broaden your knowledge under the expert guidance at Webskitters Academy! We provide complete training to transform you from a novice to an expert in the market.
“I think the Software Testing Course is apt for me. Webskitters Academy has offered me the best experience for online training. Every learner can learn more about several concepts. The assignments to write Test cases and Test case Scenarios equipped me to learn to work on real-time projects. I got a job at a reputed firm with the help of the placement cell of this institute. I am extremely happy.”
“After the Software Testing course at Webskitters Academy, I now have the confidence to face testing interviews. I trusted the institute and here they gave me the best of the best. The assignments are great and they helped me to think out of the box and come up with new questions. I am extremely happy and satisfied and also got a job through this institute.”
“I am 100% content with the Software Testing course at Webskitters Academy. The professionals explain every question and doubt that aroused in my mind. The entire course is explained the thorough and step-by-step process. It is very professional but the learning method and environment are very friendly.”
“I am glad that I took the decision of joining Advanced PHP and MVC (Laravel) online course at Webskitters Academy. I joined the course during the lockdown, to learn programming. However, it is turned out to be more than just a course. It became my passion. The tutors were so good and encouraging. I even got a good placement during the pandemic, just a few days after the completion of the course.”
“Learnt PHP Laravel under Swarup Kumar Saha Sir. He is very helpful and excellent trainer. I am done this course online. The training was good I improved my coding skill and also improved my communication skills.”
“Learnt PHP With Laravel under Swarup Kumar Saha Sir. He is an excellent trainer. The training was good I improved my coding skills as well as communication skills.”
“I did training on Android App Development using Core Java from here. I guess, I couldn’t have find a better faculty than Swarup Sir. The thing I liked the most is that he is very friendly and always eager to help us on any terms. Apart from teaching us Android in a very great way, he helped us in personality development too, as he always motivated us in a good way. So, I guess these training days were not only for learning but also for finding our own skills and mastering them. Thanks to Webskitters for providing us this opportunity. I would like to learn more from here.”
“I had a fantastic experience with this academy. I trained by professionals. I learned Android App Development using Core Java properly and got a job very early in a reputed software company through this academy. Many many thanks to Amit sir, Riyanka ma’am and Debjit sir.”
“I have done Android App Development using Core Java training from this academy for one month. They cover most of topic in one month. Trainer (educator) was very supportive and polite.”
“Right after completing my Android App Development with KotlinCourse at Webskitters Academy, I landed a job in a reputed firm. This was an incredible experience for me as before that I was looking for a job for almost a year but failed to get any. Thanks to the professional training I took from the experts that helped me start my career instantly.”
We are glad to have hired the students from Webskitters Academy! We have to mention that they are trained to be the professionals. From the first day onwards, they have shown their excellence, and it is very impressive. Our team is also happy with their involvement and performance. Looking forward to hire more excellent students from them!
We are happy with the quality of training that the Webskitters Academy students have received. Few of them have been on-board with us recently and they have impressed us. We would recommend our associates to hire freshers from this institute, they make sure that the students are well-trained and prepared for the industry.
We hired the students from Webskitters Academy and to our surprise we found out that they are brilliant in their work! No way we can call them students, they are professionals. They know their work, have the skills and are well-groomed for the profession. Thanks, Webskitters Academy for such a comprehensive training!
“It was for the first time that we hired students from Websitters Academy. We had some doubts regarding freshers, but to our surprise, they are very good in their work. They know exactly how to do a particular job keeping the industry standards in mind. Our organization is happy to have them. Their codes, and development skills are perfect, suitable for the industry. It proves that they have been trained comprehensively. Good work by the faculties of Webskitters Academy. We will definitely recruit more young talents from you.”