PHP Variables

By

When you start to write something with PHP, which is something you will often use the variables already have to start to make sense and put in your mind. The issue of PHP variables is not so much obsessive and difficult, but in general we cannot predict exactly what works and what we can do without a concrete example.

 

If you define PHP variables, you run a market as an example. The cars of the companies will help you to provide your products. What we are loading into these cars is supplies, drinks and so on and so forth. In other words, this car brings us something without being empty. Basically, this works well in PHP. It brings us what we constantly put on it without ever being empty.

 

What Is PHP Variables ?

First of all, we need to know that if we want to create a change in PHP, the variables start with the $ sign. After starting with the dollar sign, we should keep in mind that the variables are case sensitive. When creating a variable in PHP, the variables start with a letter or an underscore. Variable names cannot start with a number and can only contain alphanumeric characters such as A-a, 0.9, _.

 

With the following examples, you will be able to comprehend the topic of PHP Variables and have a look at what the variables do.

 

Creating and Printing PHP Variables

If we want to create a variable in PHP, we should start with the $ sign. All we need to do is set a variable name and then assign a value to the variable. For this:

<php
$name = "Gokmen";
echo "My name is $name";
?>

In the example above, we see how to assign a value to the name variable and then print it inside the echo command. Variables do not just print what we have assigned directly above, but we can do a lot of things with the help of variables, we can print these things and do a lot more different things with the variables.

 

Adding Numbers with PHP Variable

We were able to successfully print the name of Gokmen in the name above. But if we now define two numbers as variables, wouldn’t it be fun to have mathematical operations between these two numbers printed on the screen? Yes, we are learning to progress further.

<?php
$number1= 5;
$number2=10;
echo $number1 + $number2;
?>

When we take the codes above and run them on our computer, the mathematical operation we have written will remain valid and we will print 15 values ​​on the screen. But the important thing is that simple operations such as addition and subtraction will be easy to do while the main issue we need to know if a mathematical operation is necessary to know the value of Global and Static and it is best to use.

 

PHP Global Variable

Now you’re going to say what’s happening. Let’s explain immediately, sir, we are assigning a variable and if we do not change the expression in this variable, we need to use the global variable. So to summarize, this variable is used to access the global variable from within a function. Let’s explain:

<?php
$number1 = 5;
$number2 = 10;
function firstTest(); {
global $number1, $number2;
$number2 = $number1 + $number2;
}
firstTest();
echo $number2;
?>

PHP also stores all global variables in a directory called $ GLOBALS [index]. The index is named after the variable. This array is also accessible from within functions and can be used to directly update global variables. If we are to reformat the above example;

<?php
$number1 = 5;
$number2 = 10;
function myTest() {
    $GLOBALS['number2'] = $GLOBALS['number1'] + $GLOBALS['number2'];
} 
myTest();
echo $number2;
?>

In the example above, the value output will be 15. If you take a look at the Static value below, you will understand the basic difference between the two better.

 

PHP Static Variable

Normally all variable values ​​are not automatically stored and deleted when a function is completed or executed. However, sometimes we want to use a local variable that will not be deleted, and we need the static variable so that the value is not forgotten.

<?php
function myTest() {
    static $number = 0;
    echo $number;
    $x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>

In the example above we wrote the echo command into the function and it will automatically write it when the function is executed. In the example above, we have run the function three times and it will print the value of 0 when the run value of the function is 0 and the value 1 when the second runs, and 2 when the third runs. Thanks to the Echo command, we have ensured that these values ​​are overlapped.

 

Thanks to the PHP tutorials, you are slowly learning PHP, and you should be able to start the application by gradually starting the practice on your own computer. In this changing world of technology, now is the right time to learn something. Go ahead and start!

Leave a Comment

Your email address will not be published.

You may also like