Just another PHP Basics tutorial for beginners- Part 1

Arun Yokesh
4 min readApr 5, 2018

--

Photo by Henri L. on Unsplash

3 letters together constitute the world most popular programming languages for web development. PHP — Hypertext Preprocessor

  • Inbuilt Functionality
  • Security
  • Supports different database and data formats

To run a PHP file

  • Install WAMP server (or) install XAMPP server for Windows.
  • Install LAMP for LINUX machines.

Eg: let as consider WAMP sever ,for WAMP the default host name is “localhost”.

  • Find the www folder C:/ (or) where you installed the WAMP and create a new folder. The folder name should be without any space .
  • Place your PHP file inside the folder and every PHP file must have the file extension of called(.php).
  • Then open your browser and type your hostname (eg: localhost).
  • The WAMP page will show in that find your PHP folder under projects and open your folder , then your PHP file.

Basics of PHP

PHP file should have the extension of .php, Use text editor, notepad, dreamweaver, sublime editor, notepad++, and other editors to create and edit the PHP files.

PHP Tags

<?php-----Your PHP code here----?>

Defining a Variable

Every PHP variable must be defined with the symbol called “$”

<?php    $variable; ?>

Initialisation of a variable

<?php$variable=”one”;?>

every PHP code line will ends with the semicolon (;)

Assign value to multiple variables

<?php$variable_one=”first”;
$variable_two= $variable_one;
?>

$variable_two value will be first

PHP Basic Datatypes

╔═════════════════╦══════════════════════╗
Examples Name of the Datatype
╠═════════════════╬══════════════════════╬
║ $validity=true; ║ Boolean ║
║ $size=15; ║ Integer ║
║ $temp=4.44; ║ Float ║
║ $var=null; ║ NULL String ║
║ $cat=”animal”; ║ String ║ ╚═════════════════╩══════════════════════╝

PHP Comment Line

Comment line is used to comment between the codings for our knowledge to know some additional informations

In php 2 symbols are used to give comment they are

  • //comment line
  • #comment line
<?php//this is php file//this line doesn’t print while running#thank you?>

Print PHP Variables

<?php$string =”Hello World!!”;
echo $string;
?>

PHP is case sensitive so give your variables carefully. Give echo to print the variable values.

OUTPUT: Hello World!!

<?php$string =”Hello World!!”;
print $string;
?>

php is case sensitive so give your variables carefully. use print keyword to show the value

OUTPUT: Hello World!!

First you have to initialise the variable

<?php$string =””;
//also give $string =null;
echo $string;
?>

OUTPUT:

Unset a variable

<?php$var =’print the character’;
echo $var; //output is print the character
unset($var);
echo $var; //output is --------null----------
?>

Print String along with PHP variables

<?php$name=’Arun yokesh’;
echo “my name is”.$name;
?>

To join php variable with other thing use dot(.)

OUTPUT: my name is Arun yokesh

(.) is used to attach a php variable along with the sentence

<?php$price=500;
$discount=200;
echo “brand new pencil is Rs”.$price.”after discount”.$discount.”rupees”;
?>

OUTPUT: brand new pencil is Rs 500 after discount 200 rupees

String Concadination

<?php$numbers = “”;
$one=1;
$two=2;
$three=3;
$four=4;
$numbers .=$one.$two.$three.$four;
echo $numbers;
?>

OUTPUT: 1234

<?php$numbers = “”;
$one=1;
$two=2;
$three=3;
$numbers .=$one;
$numbers .=$two;
$numbers .=$three;
echo $numbers;
?>

OUTPUT: 123

to join text with old stored data used . before “=” operator.

PHP Operators

╔═════════════╦═══════════════════════════════╗
Operators Meanings
╠═════════════╬═══════════════════════════════╬
== ║ Equal to ║
!= ║ Not equal to ║
> ║ Greater than ║
║ >= ║ Greater than or equal to ║
║ < ║ Less than ║
║ <= ║ Less than or equal to ║
║ === ║ Equal to and of the same type ║
╚═════════════╩═══════════════════════════════╝

PHP loop operators

╔═════════════╦═════════════╗
Operators Meanings
╠═════════════╬═════════════╬
║ && ║ AND ║
║ || ║ OR ║
║ ! ║ NOT ║
╚═════════════╩═════════════╝

PHP Arrays

<?php$fruits=array(‘apple’,’banana’,’grape’);
echo $fruits[1];
?>

OUTPUT: banana

<?php$fruits = array(
‘a’ => ‘apple’,
‘b’ =>’banana’
);
echo $fruits[‘a’];?>

OUTPUT: apple

<?php$data = array(
‘username’ => ‘arun’,
‘password’ => ‘secret’,
‘host’ => ‘192.168.1.4’
);
echo $data[‘host’];?>

OUTPUT: 192.168.1.4

To get the array size use

<?php$data = array('a','b');
$size= count($data);
echo $size;
?>

OUTPUT: 2

Get array values

<?php$data_store = array(‘one’,’two’,’three’);
echo $datastore[1];
?>

OUTPUT: two

<?php$data_store = array(‘one’,’two’,’three’);
foreach($data_store as $i){
echo $i “\n”;
}
?>

\n is used to produce a new line

OUTPUT:

one
two
three

PHP statements and conditions

break; continue; exit;

If conditions

<?php if(condition)
{
-----code-----
}
?>

If else statement

<?php if(condition)
{
-----code 1-----
}
else
{
-----code 2---
}
?>

Switch Case Statement

<?php$today=’monday’;
dwitch($today)
{
case ‘monday’:
echo ‘1’;
break;
case ‘tuesday’:
echo ‘2’;
break;
case ‘wednesday’:
echo ‘3’;
break;
case ‘thursday’:
echo ‘5’;
break;
case ‘friday’:
echo ‘6’;
break;
case ‘saturday’:
echo ‘7’;
break;

?>

PHP Loops

while loop

<?phpwhile(condition)
{
-----code----
}
?>

Do while Loop

<?phpdo
{
-----code-----
}
while(condition)
?>

For loop

<?phpfor(initialization;condition;increment/decrement)
{
-------code-----
}
?>

for example

<?phpfor($i=0;i<=3;i++)
{
echo $i;
}
?>

OUTPUT : 0123

PHP Functions

<?phpfunction function_name()
{
--------Execution code----
}
function_name();
// call function
?>

For example

<?phpfunction print($str)   // argument passing in the function
{
echo $str;
}
print(‘Hello world!’);?>

OUTPUT : Hello world!

will be continued in next part : https://medium.com/@arunyokesh/just-another-php-basics-tutorial-for-beginners-part-2-433bb28de0d0

If you like this article. click the applause button! 👏 Follow me on Github, Twitter, Facebook.

--

--

Arun Yokesh
Arun Yokesh

No responses yet