Tuesday, 15 October 2013

Code Indentation in php


Code Indentation in php

Code Indentation in php programming languages. This is a tutorialabout code indentation. Now this tutorial i will explain what is code indentation is and why it is used ? For example see screenshot1 now this file works perfectly. Now Screenshot2 is output of file shown in screenshot1. In screenshot2 Output is You’re Over 21 Yes,1 is equal to 1! Now lets see code part in screenshot1 If name is equal to alex it will run part
Code Unintended :-
Code Indentation in php
Code Indentation(Screenshot1)
Code Indentation in php
Code Indentation(Screenshot2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$name='Alex';
$age = 21;
if(strtolower($name)==='alex') {
if($age=21) {
echo 'You\'re over 21';
if(1===1) {
echo'yes, 1 is equal to 1!';
}
}
}else {
echo 'You\re' not Alex';
}
and If we change name is equal to Dale Screenshot3.
Code Indentation in php
Code Indentation(Screenshot3)
Output will be You’re Not alex! screenshot4
Code Indentation in php
Code Indentation(Screenshot4)
It will run lower part of the code
1
2
3
else{
echo 'you\'re not Alex!';
}
Now problem is in this code there is not code indentation therefore its hard to say where if statement starts and when if statement is close and where is else statement is. So we Need to do modification to program. It will be lot harder if we not do code indentation. Now you seen in screenshot1 its a 14 lines of program or script you can say imagine if we have 14 thousand line program or scripts and we do not used indentation. you can imagine how  messy it look. Now let we write code in proper manner.
Code Indentation Code (ScreenShot5) :-
Code Indentation in php
Code Indentation(Screenshot5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$name='Alex';
$age = 21;
if(strtolower($name)==='alex') {
   if($age=21) {
     echo 'You\'re over 21';
     if(1===1) {
     echo'yes, 1 is equal to 1!';
     }
  }
}else {
echo 'You\re' not Alex';
}
Now in this code what i had done is after first if statement i had done code indentation or second if statement inside second ifstatement i had code indentation to third if statement. Now you can see view of code indentation how its change look of the file. So always use code indentation to you code. its a standard methods to write codes. Its very important when you distributing code or you need other people to read code. you may understand it works and it may be work in fact  but others can not understand and its also difficult to make quick modification to your code without code indentation.

Read More Extra Tutorials

No comments:

Post a Comment