Wednesday 26 March 2014

Arrays in PHP | Chapter 3

 - Indexed arrays - Arrays with numeric index or numeric keys.

Understanding Numeric Index Number -  Every value in the array is assigned with an index number. Below image shows how an index number is assigned to a set of values. The first value is assigned with a index value of 0 as the index number always starts with Zero.


php indexed arrays












Example -

<?php

$numbers = array("one","two","three");

for($i=0; $i<sizeof($numbers); $i++) {

echo $numbers[$i]."<br>";

}

?>

Output:

one
two
three




  - Associative arrays - Arrays with string index values or keys. Here we can manually define the value as keys instead of 0's & 1's which we saw in Indexed arrays.The figure below includes the age of few candidates in an array, going by the indexed array will not be the best choice, instead we can use their names as the key. Accordingly we can associate the key to their ages.


php associative arrays











Thursday 26 December 2013

PHP Variable & Data Types | Chapter 2 Cont.



Boolean - It is a data type containing two values which is TRUE (1) & FALSE (0) . They are mainly used to compare conditions for use in conditional statements

php boolean













 General Syntax -
$variable=True;
$variable=False;



 Integer - It is a data type containing numbers. Integers can be defined in three formats which is Decimal, hexadecimal & octal. An integer can be both positive or negative.

General Syntax -
$variable = decimal number;
$variable = negative number ;
$variable = octal number ;
$variable = hexadecimal number;




Example -
<?php
$variable = 1234; // decimal number (base 10)
$variable = -123; // a negative number
$variable = 0123; // octal number (equivalent to 83 decimal) (base 8)
$variable = 0x1A; // hexadecimal number (equivalent to 26 decimal) (base 16)
?>




Float - A number containing a decimal point is known as Float Value. They can be positive or negative.

General Syntax -
$variable = 10.365;


Tuesday 17 December 2013

PHP Variable & Data Types | Chapter 2



What Are Variables?
  •  Variables are used to store data.
  • A variable in PHP always starts with a $ sign.
  • PHP Variable can stored 8 types of data types.


php variables and data types











Rules of Variables
  • A variable name should not start with any special character or spaces. 
  • A variable name should start with a letter, number or underscore followed by any letter, number or underscore. An underscore is not treated as special character. 
  • A variable in PHP doesn't know in advance that what type of data type it will be storing as in c++,c. 
  •  Variables are not necessary to be declared before assignment of any value.  

php variables and data types





General Syntax -
$variablename=value;

Example 1-
<?
$test=1;
 echo $test;
?>

Output - 1



Example 2 -
<?php
$test=1;
$test1=$test;
echo $test1;
?>

Output - 1

In this example we create a variable test & store an integer to it and then we create an another variable test1 & assign it the value of test.  Now $test1 holds or stores the value of test which is 1. This example was just to show that the value of one variable can be stored in the another just by pointing the variable name instead of the value as shown in the example.


Example 3 -
<?php
$one=1;
$two=2;
$result=$one+$two;
echo $result;
?>

Output - 3

In the above example we have created two variables & assigned integer values to it. Now we create an another variable result & assign it the value we get after the addition of the first & second variable. For the addition of two variables we use the simple addition arithmetic operator. 1+2 is 3 so 3 gets stored in variable $result.



Saturday 14 September 2013

Writing your first "Hello World" script | Chapter 1 Cont.



Firstly create a file with any random name with an .php extension & save it in the web root directory.

introduction to phpintroduction to php












 A PHP block started <?php tag & ends with ?>. There are two basic statements for showing an simple Hello World! message on the browser.

Using Print -

<?php 
 print "Hello World!";
?>

Using Echo -

<?php
echo "Hello World!";
?>


Write & save the above php code. When you run the file on your browser, it will show the expected result which is Hello World!.

introduction to php












One main thing to know before you move further is that HTML can be embedded inside php & php can be embedded inside html.


  • Using PHP inside HTML

<html>
<head>
<title>TEST</title>
</head>
<body>
<?php echo "PHP-Sec"; ?>
</body>
</html>



php hello world scriptintroduction to php



But the interesting part is when you look at the page source code, you will not find any php code but instead HTML source code. As we have read in the previous introduction chapter that php is an server side scripting language. The code is executed on the server side & the output is converted & shown as HTML on the client side. No one can view the original source code.




  • Using HTML inside PHP

<?php
echo "<html>";
echo "<head>";
echo "<title>TEST</title>";
echo "</head>";
echo "<body>";
echo "<h1>PHP-Sec</h1>";
echo "</body>";
echo "</html>";
?>


introduction to php




Now let's have get forward to the next step which is Comment Line. Comment line is used to comment out something which is ignored while the script gets executed. It doesn't effect the PHP code. The basic purpose of using comment line are
-  Writing Code Description
-  Algorithm Working
- Writing credits
and so on.

Tuesday 10 September 2013

Introduction to PHP | Chapter 1

Welcome to the very first tutorial of PHP. In this chapter i will be giving an brief introduction to php language.


  • What is PHP?
- It was originally developed by  Rasmus Lerdorf in the year 1994.

- PHP is a server side scripting language just like ASP & unlike c, java etc. The script is executed on the server side & is shown as HTML on the client side thereby hiding the original source code.

php-sec










- PHP is a loosely typed language as we don't need to specify a data type while creating a  variable like c#, c etc. We just create a variable & assign it integer value.
- PHP is an Compiled language after PHP4.
- It is an Open source software which supports various Platforms like Windows, Unix, mac etc.3
- PHP/Apache is pre-installed in OS X.
- It Supports various databases like  MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.

php-sec