Learn PHP in just 24 hours – Part II

Learn PHP in 24 hours part 2

In this part, I am going to help you understand the fundamentals part of PHP like Data types, Variables, Operators, Comments and some basic logic’s of programming.

Data Types

In any programming language, Data types means the classification or categorization of data on the basis of its attributes. There are different types of data types in PHP and they are Integer, Boolean, Float, Double, Char, String etc. So, in programming, we use data types based on our needs in programming like if you want to store numbers then you declare variable as a integer data type, if you want to store floating numbers like 4.02 then you declare variable as a float or double data type and if you want to store name, address or string then you declare it as string data type.

Variable in PHP

A variable is a name given to a memory location that stores data at runtime. In PHP, we can declare variable just by starting with dollar sign.

For example:

$hello

Here, $hello is a variable and variable names are case sensitive: this means that $HELLO and $hello is different.

All variables names must start with a letter following others characters and must not contain any spaces.

PHP determines the data types depending on the attributes of the provided data.

For example:

<?php

$hello = 1;

echo $hello;

?>

Output : 1

Here, $hello is a variable of integer data type.

<?php

$hello = 3.14;

echo $hello;

?>

Output: 3.14

Here, $hello is a variable of floating data type.

Constant in PHP

A constant is a variable whose value cannot be changed at runtime. Suppose, we are going to write a program which uses the value of PI then it is better to create a constant of that PI.

For example:

define(‘PI’,3.14);

It defines that constant PI with value 3.14 and once you define this you cannot be changed the value of PI later in the program.

Operators in PHP

An operator is a symbol that tell the compiler or interpreter to perform specific mathematical, relational, comparison, logical operations and produce final result.

There are different types of operators in PHP programming. They are:

  1. Arithmetic Operators

Arithmetic Operators are used to perform arithmetic operations on numeric data. For example: Addition(+), Subtraction(-), Multiplication(*), Division(/), Modulus(%),Negation(-), Concatenation(.).

  1. Assignment Operators

Assignment Operators are used to assign value to variables. For example: =, +=, -=, *=, /=, %=,  .=  etc.

  1. Comparison Operators

Comparison Operators are used to compare values and data types. For example: Equal(==), Identical(===), Not Equal (!= or <>), Greater than (>), Less than(<), Greater than or equal(>=), Less than or Equal(<=).

  1. Logical Operators

Logical Operators evaluates expression to either true(1) or false(0). For Example: and (&&), or(||), Exclusive (xor) and Not(!).

  1. Conditional Operator

Conditional Operator is also ternary operator which evaluates an expression for a true or false value and execute one of the given statements based on the result of the evaluation.

For example: Conditional Expression (?:)

If Condition is true ? Then value X : Otherwise value Y

Comments in PHP

When you are writing code, sometimes you need to write the definition of the code or login inside the code file because when you work on the same code after long time then you will not understand the logic or it takes time to understand so, to save our time or to help other programmers understand your logic, we have to write help text inside the program, In PHP, we can do so by writing comments inside the program which is ignored by the compiler.

You can write comments in PHP in two ways

  1. Single Line Comment

//This is single line comment

  1. Multi Line Comment

/* This is

multi-line comment */

Include and Require in PHP

Include

Suppose, you are developing a website that contains same menus, navigations or sidebar across all pages. So, to decrease the redundancy in code, you can create a common header then includes it in every page using include statement. Include has two variations, Include and Include_once.

Syntax:

<?php

include ‘filename’;

?>

Or,

<?php

include_once ‘filename’;

?>

Include_once is ignored if the file is already included.

Example to show how you can use Include in projects

Let’s make header.php file which includes header or navigation or toppart of the website.

<a href=”index.php”>Home </a>

<a href=”about.php”>About </a>

<a href=”contact.php”>Contact </a>

Now, in index.php file, you can call that header.php using include statement as,

<?php

include ‘header.php’;

?>

Require

Require and Require_once are two statements which are used to include file. Require_once is ignored if the required file is already been added by any of the above statements.

Syntax:

<?php

require ‘filename’;

?>

Or,

<?php

require_once ‘filename’;

?>

Key Differences between include and require

  1. Include issues a warning when an error occurs whereas require does not issue a warning.
  2. With Include, execution of the scripts continues when an error occurs wheras with Require, execution of the scripts stops when an error occurs.

So, both include and Require statements has its own advantages. The require statement should be used if the entire script cannot run without the requested file whereas include statement should be used in case of script can run without the requested file.

For example, in case of showing contact us page, it is better to use include statement but in database configuration, it is better to use require statement.

I think you have understood the fundamentals of PHP programming well enough, in this tutorial, I have just focused on practical approach or clearing out the very important topics to later focus on practical approach of programming.

If you have any confusion or problems then please let me know it by commenting your problems in the comment section below. I will solve your problems and then get back to you within few hours.

CategoriesPHP

Leave a Reply

Your email address will not be published. Required fields are marked *