PHP Lesson 5 – List of basic elements in PHP

Last updated on December 9th, 2023

What are the main elements in PHP?

Here is a short list of the basic elements of the PHP language:

– variables
– operators
– arrays
– functions
– control structures
– constants
– database connectivity

Detailed information and examples of all these elements will be given in other lessons. Here is just a brief description of each of them:

What is a variable?

Variables are used to store data values (text, numbers, etc.). In PHP, variable names start with the dollar sign $ followed by the variable name, for example: $variable. The variables are like containers where you can store information. Imagine them as labeled boxes. Variable names can contain lowercase and uppercase Latin letters, Arabic numerals, and an underscore. Variable names can only start with a letter or an underscore. You can set the variable value using the equals sign, like this:
$variable = “value”; .

What is an operator?

Operators are used to perform various actions. PHP includes a wide range of operators. The main types of operators are:

– arithmetic operators (+, -, *, /,%)
– assignment operators (=, +=, -=, *=, /=)
– comparison operators (==, !=, <, >, <=, >=,===)
– logical operators (&&, ||, !)
– concatenation operator (.)

Think of operators as tools for doing math and comparisons. You can add numbers (+), subtract them (-), multiply them (*), and more. You can also compare things: Is one thing equal to another? Is it bigger or smaller than the other? Etc.

What is an array?

Arrays are used to store multiple values in a single variable. PHP supports indexed arrays, associative arrays, and multidimensional arrays. Arrays are like lists that can hold many pieces of information. Imagine you have a shopping list – in this case you can put all your items in one array. The format of arrays is the word array followed by round brackets in which the values are placed, like this:
array(“value1”, “value2”, “value3”).

What is a function?

Functions in PHP are blocks of reusable code that perform specific tasks. You can create your own functions or use built-in PHP functions. The function name is followed by round brackets, for example: function(). Functions are like mini-programs inside your program. For example, there’s a built-in function called strlen() that tells you how long a word is, there is a built-in mail() function that you can use to send an email, etc.

What is a control structure?

PHP provides control structures like if statements, switch statements, loops (for, while, do-while, foreach), and conditional operators (ternary operator) for decision-making and looping. The control structures are like instructions for your program. You can tell PHP to do something only if a condition is met (like “if it’s raining, take an umbrella”), to do something while the condition is met (like “keep the umbrella open while it rains”) and so on. You can also make PHP do something over and over again (loops). The name of the control structure is followed by round brackets and curly brackets, like this: if(){}. The round brackets contain the condition, and the curly braces contain the code to be executed if the condition is met, like this:
if (condition) {code to be executed if the condition is met}.

What is a constant?

Constants are like variables, but their values cannot be changed once they are defined. They are defined using the define() function or using the const keyword. So, constants are like fixed values, once you set them, they can’t change. It’s like having a special rule you always follow. Constant names, like variables, can start with a letter or an underscore, but unlike variables, constant names are not preceded by a dollar sign. It is considered good practice to use uppercase letters for the names of constants to make them easier to distinguish from variables and also to improve the readability of PHP code: CONSTANT.

What is database connectivity?

PHP can connect to various databases like MySQL, using database-specific extensions or libraries. So, that means that PHP can talk to databases and store information in them. It’s like storing books in a library or asking a library for a specific book and getting the information you need.