PHP syntax basics


How to work with variables

Any variable must first be declared, that is, given a name and a value.

In PHP syntax, a variable name is written in Latin characters, but the first character must always be a dollar sign $, followed by the name.

It is not allowed to start a variable name with a number, and also to use any values ​​other than alphabetic characters and underscores.

Examples of valid variable names:

$age;

$favorite_color;

$name2.

Examples of invalid names:

age – dollar sign at the beginning is forgotten;

$42 – starts with a number;

$my-age – contains a hyphen.

Assignment

An empty variable won’t be very useful. So let’s just put something in there. This action is called an assignment operation.

This is how assigning information to a new variable looks like:

$favorite_color = “green”;

The equal sign in PHP is an operator and always means an assignment operation.

Here we have written the word “green” to a variable called favorite_color.

Please note that we have enclosed the word green in quotation marks. Quotation marks are always strictly necessary when it comes to using text. But if not text, but a number is placed in the variable, then quotes are not needed.

Example:

$favorite_number = 42;

Usage

We have learned how to store information in variables. Now let’s see how to access this information in our scenario.

After all, when we try to remember a new phone number in our head, we do it for a reason, but to call it.

This means that we memorize information in order to return to it in the future and apply it for some action – make a call, write a message, and the like.

Such logic fully works in programming. Having saved the information once, it can be used in the future to perform various actions. Such an action can be the display of this information on the screen.

Let’s rewrite the already familiar script so that it uses variables.

<?php

$welcome_text = “Hello world”;

print($welcome_text);

Now this simple script has two steps. On the first one, we declare (initialize) a new variable and put text there. In the second step, we use this variable by referring to it by name in the context of calling the display function. That is, now the print function does not receive the text directly, but a variable that contains this text.

Want to practice using variables in PHP? Then complete this task.

Concatenation

This terrible word is the operation of gluing several lines into one. Suppose we have two already familiar variables: $favorite_color and $favorite_number, and we are going to print them on one line to get the following result:

“My favorite color is green and my favorite number is 42”

where the color value is stored in $favorite_color and the numbers in $favorite_number”.

To get such a string, you must use the concatenation operator – . (dot). It takes two strings or variables containing a string as operands and then returns a new string.

It looks like this:

<?php

$united_string = “My favorite color is ” . $favorite_color . “, and the favorite number is ” . $favorite_number;

print($united_string);

In this activity, you will learn about concatenation in practice.

Data types

It has already been mentioned above that the data placed in a variable can be a string of text or a number. In reality, there are many more such data types, but at first we will work with only a few. However, it is still important for us to understand now what “data types” are.

A data type in a programming language defines the allowed values ​​as well as the allowed operations on data of that type.

For example, arithmetic operations can be performed on data of type “integer”, but not on data of type “string”. It is not possible to split string by string. In addition, numbers can only be within certain limits (in the 32-bit version of PHP, the largest number is 2147483647), and strings must be in quotes.

Operators

An operator in programming is something that takes one or more values.

For example, in arithmetic there are such operators: +, -, /, *. What does addition, subtraction, division and multiplication mean. The operator always works only in pair with two values, for example, adding two numbers (operand) or multiplying them with each other.

You can practice using arithmetic operators in this activity.

PHP supplements the operators already familiar to us from arithmetic with several new ones:

the already familiar assignment operator =, which is used to assign a value to a variable;

comparison operators are very useful: ==, !=, >, < – equal, not equal, greater than and less than;

% is the remainder operator when one number is divided by another. For example: 5% 2 == 1; // true.

Conditional constructs

Sometimes, depending on the condition, you need to perform different actions. For this, the if statement is used. For example, we asked for the gender of the visitor to our page, stored it in a variable, and now we want to display a different greeting, depending on whether it is a man or a woman. This is where the if and else statements come in handy.

<?php

if ($gender == “male”) {

    print(“Greetings, my Lord!”);

} else {

    print(“Greetings, O Lady!”);

}

The example shows that the conditional construction consists of three parts:

  • if condition is an expression that returns a true or false result;
  • if block – lines of code that will be executed if the condition returned true;
  • the else block is the lines of code that will be executed if the condition returned false.
  • Note that each block is framed and delimited by curly braces.

To consolidate the material, go through this simulator. It explains in detail how to use conditional constructs.

Leave a Reply

Your email address will not be published.