DEV Community

sannilincoln
sannilincoln

Posted on

PHP language building blocks

In this article, I will walk you down on how to set up PHP, and the basic building blocks of PHP.
PHP is widely preferred than other web scripting languges because:
#It runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).
#it is compatible with almost all servers used today (Apache, IIS, etc.).
#It supports a wide range of databases.
#It is free to use.
#It is easy to learn and runs efficiently on the server side.
PHP can be installed using a software package called xampp which has an Apache server, database(MariaDB) and PHP itself. the software allows easy installation of PHP as the whole package needed to start PHP is being installed together. after installation, to start using PHP. you will launch xampp and run it as administrator(on windows). then the control panel dashboard would appear. then, you click on Apache and MySQL services to start running. next is to head over to your browser and type localhost on your address bar, this will take you to the xamp homepage by default, this shows everything is set. to start your own coding, head over to local drive(C drive), find a xamp folder and navigate to htdocs folder, and create your PHP file there e.g,(home.php). then head over to your browser and set the address to the name of the PHP file you created. e.g (localhost/home.php).
PHP codes are wrapped in <?php and ?> these are symbols used in telling the compiler where the execution of the PHP code starts and stops. A PHP file, can contain an HTML code but you have to denote where your php code starts and where it ends using the PHP delimiters.
To initialize a variable in PHP, you have to start with the dollar sign $, then your variable name.php variable is case sensitive and must not start with a number or an underscore. Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.
PHP, just as other web scripting language supports various data types such as:
string: which is a sequence of characters, and is any text put inside double or single quotes.
integer: integers are numbers without decimal and they are not put inside quotes, unlike strings. they can either be positive or negative.
Float: is number with a decimal point or a number in exponential form.
boolean: this data-type represent two possible states: TRUE OR FALSE.
PHP also has data type such as arrays, objects and PHP Resource which stores reference information to functions and other resources which are external to php e.g database call.
PHP Constants are similar to a variable but they cannot be changed during the script. they are started with either letter or an underscore with no $ sign. to create a constant, the syntax is
define(name,value,case-insensitive).
name: name of the constant.
value: value of the constant and last parameter is the boolean to indicate whether the case should be insensitive or not. The default is false. Php constants are global, which means they can be used across the entire script.
PHP has many operators which are used in performing operations based on variables and values.
Arithmetic operators such as + - /%* for basic arithmetic operation.
The assignment operator is used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right e.g x=y, x+=y, etc.
other operators include the comparison operator which returns true if the condition is true and false if otherwise e.g equal (==), identical (===), <,>, etc, the incremental operator is used to increase or decrease variable value e.g ++,--, the logical operators are used to combine two conditional statements e.g and, or, xor,&& ||. we also have string operators e.g concatenation(.) and php array operators which are used to compare arrays e.g union (+), equality(==) and conditional operator which are used in setting value based on conditions e.g ternary operator(?:), Null coalescing(??).

Top comments (0)