This course is among the free PHP courses we offer as Onur Özden Web Çözümleri. PHP Variable Declaration We will explain the topic.

Before giving information about variables, we would like to mention this. Defining variables is similar in all programming languages. In some programming languages, we need to specify a data type for variables. In some cases it is not necessary. In PHP, the data type of the variable is automatically determined by PHP at run time. Therefore, it is not mandatory to specify the data type when defining a variable. Let's keep this information in mind and learn what variables are.

PHP Variables

Variables are data holders that are used to store data and in which we can manipulate the data. We can assign many types of values ​​to variables, especially numbers and text (string).

PHP Variable Declaration

In PHP, we use the $ (dollar) character to create a variable and the = (equals) character to assign a value to the variable. At the end of each variable; It has to be (semicolon). Let's understand this with an example.

$variable1 = 'Onur Özden Web Çözümleri';
$variable2 = 4;

We defined the variable name by typing $degisken1 in the first line. We gave the variable the value "Onur Özden Web Çözümleri" of text type with the '=' operator. Since the value we gave is of text type, we enclosed the variable's data in double quotes as "value". In the second line, we defined a variable named $variable2 and gave the value 4 as number. So why didn't we put the value 4 between double quotes as in the previous line? Because the value 4 was of number type. We enclose text type values ​​between double quotes or single quotes. We print the values ​​in number type directly.

PHP data types and Differences between double quotes and single quotes in PHP We will learn about these topics in our next lessons.

PHP Variable Declaration Rules

In PHP, as in every language, we can define variables according to certain rules. The full rules are below:

  • Variables begin with the $ (dollar) sign.
  • Variables must begin with a letter or _ (underscore). It cannot start with any number.
  • Special characters (/, +, ^, " etc.) or space characters cannot be used in variables.
  • While defining variable names, reserved words used by PHP (if, break, default, new, etc.) cannot be used.
  • (a-z, A-Z, 0-9) and _ (underscore) can be used in variable names.
  • Variable names are case sensitive.

PHP Variable Declaration Suggestions

These suggestions are completely optional. However, if it is followed, it is possible to write cleaner code.

  • It is not recommended to use Turkish characters in variable names. When you add the script you wrote to Github, imagine that the variable names are in Turkish and no one understands them except those who speak Turkish. Programming languages are global. Idioms and expressions in software languages are in the globally accepted English language. Therefore, it is recommended that the nomenclatures we use are in English.
  • It is recommended that variable naming be done according to certain standards. For example, if you are developing for a CMS like WordPress WordPress code standardsIf you are developing for PHP PHP PSR standards must be complied with.
  • For readability, it is not recommended that variable names be all uppercase.

PHP Operations with Variables

Now that we have learned how to define variables in PHP, variable definition rules and recommendations, let's see what we can do with variables. In PHP, we can print variables to the screen, assign a new value next to the variable's value, combine two variables, or perform mathematical operations with variables.

Printing the Variable to the Screen

We use echo formation to print the variable to the screen. Let's find out what echo is with a small quote from PHP.net:

echoIn fact, it is not a function but a language construct, so it is not necessary to enclose its parameters in curly braces. parameters,echoIt consists of expressions following the keyword, separated by commas and not enclosed in parentheses. Unlike some other language formations,echohas no return value, so it cannot be used in the context of an expression.

Source: https://www.php.net/manual/tr/function.echo.php

Now let's print our variable to the screen.

$number = 5;
$string = 'Onur Özden Web Çözümleri';
echo $number;
echo $string;

As you can see in the example, printing the variable to the screen was very simple. We wrote the variable name using echo formation. Since echo formation is an expression/statement in PHP like everything else, we put a semicolon at the end.

Assigning a New Value to a Variable

In this example, we will learn how we can assign a new value to a variable that already has a value and add a new value next to its value.

$number1 = 10;
$string1 = 'String Value';
$number1 .= 14;
$string2 .= 'String';

We created a variable called number1 and assigned the value 10. We created a variable named string1 and assigned the value String Value. Then, we assigned the value 14 next to the $number1 value (the value was 10) with the concatenation assignment operator. In this way, the value of the number1 variable became 1014. Then, we assigned the value "String" to the string1 variable with the same operator, and the value of the variable changed from "String Value" to "String Value String".

What are operators in PHP and all operators in this lesson We will learn.

Combining Two Variables

Let's combine two variables with an example.

$number1 = 23;
$number2 = 24;
$number3 = $number1 . $number2;

We created a variable named number1 and assigned the value 23. We created a variable named number2 and assigned the value 24. Then we created a variable named number3 and assigned the values ​​of number1 and number2. In this case, the value of the number2 variable was 2324.

Mathematical Operations with Variables

In order to perform mathematical operations with variables, the variable data must be in number type. As long as this condition is met, we can add, subtract, multiply and divide variables. Let's reinforce this with an example.

$number1 = 10;
$number2 = 5;
echo $number1 + $number2; // 15
echo $number1 - $number2; // 5 
echo $number1 * $number2; // 50
echo $number1 / $number2; // 2

We gave the value 10 to the variable number1 and the value 5 to the variable number2. In the 4th line, we added number1 and number2 with the addition operator. In the 5th line, we subtracted number1 from number2 with the subtraction operator. In the 6th line, we multiplied number1 and number2 with the multiplication operator. In the 7th line, we divided number1 into number2 with the division operator.

In this lesson, we learned how to define variables in PHP, variable definition rules and recommendations, and how to operate with variables. Stay tuned for the continuation of the lessons.