PHP stands for Hypertext Preprocessor. Php is a server-side general-purpose scripting language. It is open-source available on the internet. it’s widely used in over the world also free to use for making websites, web-based software (portal, CRM, other). PHP is easy to learn and understand how it works. PHP page also supports Html tags, Javascript code, CSS code embedded code into it for providing better output.
PHP is written in the c language and developed by Ramus Lerdorf in 1994. PHP Development mainly works for server-side scripting to make dynamic output or data also you can do much more. Hypertext Preprocessor means write code or program first and PHP compiler convert code in bytecode and produce output.
What can we do with PHP?
PHP are focused to server-side scripting, we can do anything server based web application with help the of data, content, database, cookies. we can create Server-side scripting application, Client-side application and Desktop application. Desktop application create in PHP programming in not a grate choice to create.
Let start how to use PHP
Before starting to write PHP code we need to install Apache software for providing the environment execute to the code, for that we using champ software you can download it on the https://www.apachefriends.org/index.html and install it on your local machine.
Basic syntax
Before we write any code in PHP programming we need to add a tag that provides the environment for writing code into PHP pages. basically, we used to write code between starting tag <?php and ending tags ?>. check below example.
<?php //starting tags
// here we write our code
?> // ending tags
Their are more 2 type tags are available to to used.
1: <?php ?>
2: <?= ?>
3: <? ?>
Let explore more, Now how we can print data, so for that we can use some predefined tag and function which is echo, print, print_r.
<?php
echo "write anything here";
print "write anything here";
print_r([1,2,3]);
?>
In the above example the code are show how it use. echo and print work are same but echo support multi line exciting and print support single line executing.
How to write comment in php
In PHP we can use three type of comment into PHP which is below.
<?php
// single line comment
# single line comment
/*
multiples
lines
comment
*/
PHP Variables
In PHP whenever we need to assign a value or store the value in variable to require using $ special character before the name of varialable.
in the below code $name, $age is a variable which store different type of value.
<?php
$name = "sam saifi";
$age = 27;
?>
PHP data type
PHP supports different type of data type, below are describe.
- bool
- int
- float
- array
- string
- array
- object
- null
Bool type variable are store value True or False format.
<?php
$a_variable = TRUE;
$b_variable = FALSE;
?>
Int type variable are store integer , numerical, non-decimal value only.
<?php
$a_variable = 12;
// or you can use (int) before value
$b_variable = (int) 15;
//it automatically convert into the int type variable whatever type of data.
?>
Float type variable are store floating number, decimal value only. for example 15.12, etc.
<?php
$a_variable = 12.5;
?>
Array type variable are store multiple values in one variable. it also support multi dimension values and multiple data type.
<?php
$a_variable = array(1,2,3);
// multiple data type
$b_variable = array("sam",27,15.45,true);
// multi dimension value
$c_variable = array(
array(1,2,3,4),
array('sam','hitesh','sanjay','vikas')
);
// we will explore array in other post
?>
String type variable are store any type text, character, value inside the double quotes or single quotes.
<?php
$a_variable = "I am sam saifi";
$b_variable = 'I am sam saifi';
?>
Object type variable are store Instance of the class which may be custom class or predefined class. it’s most using in the OOPs pettern.
<?php
class car {
public $name = "";
}
$model = new car;
$model->name ="maruti";
?>
NUll type variable is special used when dont have value for assign so in case we used null .
<?php
$a_variable = NULL;
?>
PHP data variable Scope.
Whenever assign a variable into the php except inside the function and class it a global variable There are three type of variable scope.
- Global scope
- Local scope
- static scope
<?php
$a = 5; // global variable
function test(){
$b =3; //Local value
}
function test(){
global $a = 5; // also assign global variable with help of "global" keyword
static $b = 5; // assign static variable with help of "static" keyword for incresing reusibility
}
?>
Constants Variable
One a constants are define it will never changed. for defined constant variable need to used const or define(). constants are supported all data type to assign
<?php
const TEXT = "hello sam";
//or
define("TEXT",'hello sam');
//
?>
PHP Operators
PHP operators are the some kind of it take value then operate predefined function in the background and provide us sorted or calculated data. below are available various type of operators.
Arithmetic Operators are use for making calculation between one or more variable. check the example below.
<?php $a = 1; $b = 2; //Additional operation $c = $a + $b; // output: 3 //Substract operation $c = $a - $b; // output: 1 //Multiply operation $c = $a * $b; // output: 2 //Division operation $c = $a / $b; // output: .5 //Modulo operation $c = 5 % 2; // output: 1 //Exponentiation operation $c = 5 ** 2 // output: 25 ?>
Assignment Operators are used to assigning the value for a variable with some kind of calculation.
<?php $a = 2; $b = $a+1; $b += $a; $c = ($d= 5)+$b+$a; $c *=$a; $c = $a ?? 5; ?>
Comparison Operators are allow to compare between two or more variables.
<?php $a = 1; $b = 2; $a == $b // output: false // $a Equal to $b $a != $b // output: true // $a Not Equal $b $a > $b // output: false // $a graterthen to $b $a < $b // output: true // $a lessthen to $b $a >= $b // output: false // $a Graterthen Equal to $b $a <= $b // output: false // $a lessthen Equal to $b
Incrementing/Decrementing Operators are used for increase or decrease value using pre or post method.
<?php $a=1; ++$a; //pre method // output: 2 $a++; //post method //output: 3 --$a; //pre method //output: 2 $a--;//post method //output: 1
String Operators are used to connecting two or more strings into one single string.
<?php $name = "same"; echo $fullname = $name.' saifi'; // Output: Sam Saifi
hope you like this information to you, if you wanna learn more please checkout our other post.
Click here to see our post for Git and Github.