PHP for Beginners

PHP Variables

Definition: A variable is an identifier that denotes a storage location used to store data value.

Explanation: A variable is like a container that has a name and is used to store some data. Variable is most basic thing that you will be using whenever your code. Since you will always have some data to process, variable is where they get stored during the course of execution of the script.

Follow these rules while naming the variables:

  • All variables in PHP are denoted with a leading dollar sign ($).
  • Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be evaluated on the right.
  • Variables can, but do not need, to be declared before assignment.
  • Variables names must not start with a digit, Eg $123

Where to use variables: Almost everywhere, whenever you need to store some value, you will be needing variable.

PHP Constants:

Definition: Constants refers to the fixed value that don’t change during the course of execution of the script.

Explanation: Whenever you think about constant, think about Eiffel Tower or some thing similar to that which is fixed and not going to change, or a value that you want to be fixed and not to be changed.

Where to use constants: Lets take a scenario where you need to store π(pie) value a fixed value to some container, or where you want to use your SSN number or website address which you don’t want to be changed.

Printing Output to screen

To display the result on the browser or other output device PHP has its own in built function. Lets have the look at them.

Data Types:

Definition: Data types refers to types of data that can be stored that in a variable.

Explanation: Let’s think this way, Human Being, Animals, Insects all have their own characteristics, which differentiates one from the others, same is the case for data type. They can take data to their range and perform operations accordingly. PHP has a total of eight types: integers, doubles, Booleans, strings, arrays, objects, NULL, and resources

  • Integers are whole numbers, without a decimal point, like 495.
  • Doubles are floating-point numbers, like 3.14159 or 49.0.
  • Booleans have only two possible values: TRUE and FALSE.
  • NULL is a special type that only has one value: NULL
  • Strings are sequences of characters.
  • Arrays are named and indexed collections of other values.
  • Objects are instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

N.B: By Default PHP automatically sets a data type for a variable.

Type Casting

It refers to the conversion of a data type to another. By default PHP converts the type implicitly.

Operators

Definition: Operators is the symbol that tells the computers to perform certain mathematical or logical manipulation.

Explanation: Let’s think like way, Operators sets the relation between variables. Take a look at some of operators.

Arithmetic Operators

Operators Operation It Performs Example Results
+ Addition Explicit positive sign 12 + 43 55
Subtraction Negation 20 – 5 – 3 15
* Multiplication 3*4 12
/ Division 5/2 2
% Modulo division 5%2 1
++ Post-increment Pre-increment ++a ++b increments value by one
Post-decrement Pre-decrement –a –b decrements value by one

Assignment Operators

Operators Operation It Performs Example Results
= Assign right side to left side $a = 13 $a has value 13
+= Add right side to left side $a += 2 $a = $a + 2
-= Subtract right side to left side $a -= 2 $a = $a – 2
*= Multiply right side to left side $a *= 2 $a = $a * 2
/= Divide right side to left side $a /= 2 $a = $a / 2
%= Set left side to left side modulo right side $a %= 2 $a = $a % 2

Comparison Operators

Operators Operation It Performs Example Results
< less than 5 < 6 false
> greater than 25 > 16 true
<= less than or equals to 15 <= 5 false
>= greater than or equals to 25 >= 25 true
== equals to 5 == “5” true
=== Identical 5 === “5” false

Logical Operators

Logical operators combine other logical (aka Boolean) values to produce new Boolean values. We use it mainly in combination with comparison operators

Operators Operation It Performs Example Results
&& and (8>6) && (2>3) false. both relation must be true
|| or (8>6) && (2>3) true, if any one of them is true.
! not !false true, Is true if its single argument (to the right) is false and false if its argument is true.
xor exclusive or (8>6) && (2>3) true, Is true if its single argument (to the right) is false and false if its argument is true.
and same as &&
or same as ||

This is the end of this part, I will be writing the next part of the series soon, Till then practice, practice and more practice.

Comments (2)
  1. I’m impressed, I have to say. Actually not often do I encounter a blog that’s both educative and entertaining, and let me inform you, you have got hit the nail on the head. Your concept is excellent; the problem is something that not enough people are speaking intelligently about. I’m very completely satisfied that I stumbled throughout this in my search for something regarding this.

    1. admin says:

      Thank you

Leave a Reply

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

error: Content is protected !!