PHP for Beginners – Part 1
This tutorial is intended for people having a fair bit of knowledge in programming, and wants to learn PHP. I will be illustrating the basics and key points followed by working demo which are utmost necessary in learning the subject. Before we get going make sure you have installed the proper enviroment to run PHP or you can run some demo test on phpfiddle. But I strongly suggest you to install Xampp or Wamp or Mamp on your machines.
Spend a considerable amount of time learning the basics, will help you scratch your head less when you go through the advance part.
What is PHP
PHP (PHP: Hypertext Preprocessor) is a server-side scripting language, which can be embedded in HTML or used as a standalone binary.
How PHP works
Just like a .html or .txt file we create a .php file. Unlike an ordinary HTML file, a PHP script is not sent directly to a client by the server, instead, it is parsed by the PHP engine. HTML elements in the script are left alone, but PHP code is interpreted and executed. PHP code in a script can query databases, create images, read and write files, talk to remote servers—the possibilities are endless. The output from PHP code is combined with the HTML in the script and the result sent to the user.
PHP Tags
Let us start writing out first php program. All php script is written under php tags. Have a look.
1 2 3 4 |
<?php // this is a comment. // all your php code will be under this section ?> |
You can also use the short-open tags.
1 2 3 4 5 |
<? // you can write the tag this way also. // you can change the setting in php.ini file // just uncomment short_open_tag ?> |
Note: I prefer using the first one since it safe and always guarantee me the tags will be correctly interpreted. In using the second case that it is not compatible with XML. If you still want to use you can can go to php.ini file (located under xampp/php folder) and uncomment short_open_tag.
Using Comments
A comment is the portion of a program that exists only for the human reader. Comments can be used where you want to explain something to other or probably yourself about the section of the code. The comments will never be executed by the script.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php // This is a single line comment # This is a single line comment too // this comment will fail as it goes to next line /* So I use this multi-line Comment. but remember to end it asterix and back slash. */ ?> |
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.
1 2 3 4 5 6 |
<?php $var = 100; // declaring a variable $test12 = 125.36; $multiWords = 656; $test_another = 74; ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php define("PIE", 2.54); define("SSN", "41221551215"); // how to use constant $k = 20; $result = PIE * 50; echo "final value : " . $result; /* try this will give you error (uncomment to see) PIE = 10; */ ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php $name = "David Beckham"; $goals = 200; echo "Hello ".$name; echo "This is a multi line text. It prints $name with no problem at all. Cool "; print ("This function also output data ".$name); print "You can use this way"; echo 'when you use single qoute'. $name.' use variable this way'; $format = "The player name %s has %d goals for his country"; echo sprintf($format, $name, $goals); // if you want to print the type along with the value. // It's very useful in development mode var_dump($name); ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php $intg = 200; $dbl = 2125.32326; $str = "david"; $null = NULL; $arr = array(10,20,30); echo gettype($intg); echo gettype($dbl); echo gettype($str); echo gettype($null); echo gettype($t); ?> |
Type Casting
It refers to the conversion of a data type to another. By default PHP converts the type implicitly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $intg = 200; echo gettype($intg); settype($intg, "string"); echo gettype($intg); //implicit conversion $k = 200.33; echo gettype($k); $changed = $k."test"; echo gettype($changed); // another method of conversion $foo = true; echo gettype($foo); // outputs 'boolean' $foo = (int) 1; echo gettype($foo); // outputs 'integer' ?> |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php // How increment or decrement operator works $a = 10; $a++; echo $a; // prints 11 $a = 10; $a--; echo $a; // prints 9 $a = 10; $b = 5; // prints 6 not 7 because value of $a is first used in the //execution and then incremented. echo $a++ - --$b; ?> |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // How assignment operators works $var = 20; $var += 10; // $var is 30 now 20 + 10 // This is also known as short hand notation <br /> $var = 20; $var -= 5; // $var is 15 now 20 - 10 $var = 20; $var %= 3; // $var is 2 now 20 % 3 ?> |
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 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $testVar1 = 25; $testVar2 = 30; echo $testVar1 > $testVar2; echo $testVar1 < $testVar2; echo $testVar1 >= $testVar2; echo $testVar1 <= $testVar2; $testVar2 = "25"; echo $testVar1 == $testVar2; $testVar2 = "25"; echo $testVar1 === $testVar2; /* if you can't see the output try var_dump() */ var_dump($testVar1 === $testVar2); ?> |
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 || |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $testVar1 = 25; $testVar2 = 30; $testVar3 = 90; // use var_dump() function in case you can see the result. echo ($testVar1 > $testVar2) && ($testVar1 > $testVar3) ; echo ($testVar1 > $testVar2) and ($testVar1 > $testVar3) ; echo ($testVar1 > $testVar2) || ($testVar1 > $testVar3) ; echo ($testVar1 > $testVar2) or ($testVar1 > $testVar3) ; echo !(($testVar1 > $testVar2) && ($testVar1 > $testVar3)) ; ?> |
This is the end of this part, Next part of the series will be Published soon, Till then enjoy the code 🙂