Thursday, 18 August 2016

PHP Basics : While Loop

Hi guys,

Today lets see while loop in php, it is too simple

Lets begin your code







<?php 

$value = 1; 

while($value <= 50) 
{
    echo "$value <br>";

    $value++;


?>

Its simple and effective.

Its cool right ?




Wednesday, 17 August 2016

PHP Basics : For Loops and For Each loops

Hi guyz

Lets see about for loop and foreach loop in PHP.

Lets begin your code

First we can see about FOR LOOP

FOR LOOP





<?php

$start = 0;
$end = 100; 

// Lets print numbers from 0 to 100

for ($start; $start <= $end; $start++) 
{
    echo "$start <br>";



?>


Now lets See how to use ForEach Loop in PHP

FOR EACH LOOP


<?php 

$fruits = array("Mango", "Orange", "Pineapple", "Guava", "Apple", "Pomegranate"); 

foreach ($fruits as $fruit) 
{
    echo "$fruit <br>";
}

?>

Its so simple right, Lets do something

Now i'm gonna implement for loop instead of for each in the above program


Lets begin your code



<?php

// Lets do above program using for loop
$fruits = array("Mango", "Orange", "Pineapple", "Guava", "Apple", "Pomegranate"); 
$total_fruits = count($fruits);
echo "$total_fruits <br>";
$array_end = $total_fruits - 1;
$array_start = 0;
for($array_start; $array_start <= $array_end; $array_start++)
{
echo "Array[$array_start] contains $fruits[$array_start] <br>";
}

?>


Its awesome right, In the above program count() is used to count the number of elements in an array.

Its pretty cool right ?

Tuesday, 16 August 2016

PHP Basics : Switch case

Hi guyz

Lets see about switch case statements in php, Its just simple





Lets begin your code


<?php
$command = "4";

switch ($command) {
    case "1":
        echo "You chose number 1";
        break;
    case "2":
        echo "You chose number 2";
        break;
    case "3":
        echo "You chose number 3";
        break;
    default:
        echo "You chose out of circle";
}



In this above program, if you declare
 [1] $command=1, it will echo case 1 ,

 [2] $command=2, it will echo case 2,

 [3] $command=3, it will echo case 3,

 [4] if you declare $command other than 1 2 or 3 it will echo default


Its so simple, You can practice and have fun...!


Its cool right ?

Monday, 15 August 2016

PHP Basics : Armstrong Number

Hi dudes..

Lets check whether the number is armstrong using php.

Lets see what is armstrong number

lets take an example

Consider a number 153

take the cube of each number and add , if we get the same number then the given number is armstrong number

1 -- > 1*1*1 = 1
5 --> 5*5*5 = 125
3 -- >3*3*3 = 27

add 1+125+27 = 153

The given number 153 is an Armstrong Number


Lets begin your code





<?php
$given_number=371;
$sum=0;
//store the number in a temporary variable
$temp=$given_number;
while($temp!=0)
{
$remainder=$temp%10;
$sum=$sum+$remainder*$remainder*$remainder;
$temp=$temp/10;
}
if($given_number==$sum)
{
echo "Wow the given number is an Armstrong number";
}
else
{
echo "Oops!, It is not an Armstrong number";
}
?>



Its cool right ?

Soon we will go deep inside with PHP and we will start Python, Wordpress and So on..


Saturday, 13 August 2016

PHP Basics : Palindrome

Hi guys,

Lets do palindrome with php


Lets see what palindrome is

If a string is reversed and the reversed string is equal to the string given, then the given string is palindrome

Example

String = MOM
String_Reversed = MOM

The given string is palindrome.


Lets begin your code






<?php
// Declare a Variable 
$string = "level";
echo "Given String is $string";
// Lets reverse the string
$reversed_string = strrev($string);
// Lets check whether our string is equal to our reversed string
if ($string == $reversed_string)
{
    echo 'Wow it is a Palindrome';
}
else
{
    echo 'Oops its not a Palindrome';
}

?>


It works like a charm right......!


Its cool right ?


Friday, 12 August 2016

PHP Basics : Fibonacci Series

Hi, today lets see about fibonacci series using php

You guys know about fibonacci series, but once again i will explain you about fibonacci before entering the code

First two fibonacci number will be 0 and 1 and the series that follow will be the sum of two previous numbers

so our series will look like

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144


Lets begin your code





<?php

// Declare three variables 

$add = 0 ;
$first_fibonacci_number = 0;
$second_fibonacci_number = 1;
echo "$first_fibonacci_number, $second_fibonacci_number, ";

//Create a while loop to echo out the fibonacci series, Here lets print out first 30 fibonacci series
// followed by 0 and 1 

while ($add < 30 )
{
$next_fibonacci_number = $second_fibonacci_number + $first_fibonacci_number ;
echo "$next_fibonacci_number, ";
$first_fibonacci_number = $second_fibonacci_number ;
$second_fibonacci_number = $next_fibonacci_number ;
$add = $add + 1;
}

?>

You can run in browser using localhost/folder_name/filename.php (if you installed xampp)


Its cool right ?


Connect to MySql database using PHP

First before entering to this session, lets see what needed to be installed


  • Localhost Server - ( XAMPP or WAMPP or MAMP or LAMPP {According to your OS platform} )
  • Code Editor - whatever you like

After installing this, go to localhost/phpmyadmin in web browser 





Go to database section and create a database, for example lets create a database name Sample

lets enter into code

create a folder connection inside htdocs 

create a file database.php inside the folder connection

<?php

//lets declare a variable $connect to connect to the server

$connect = mysql_connect("localhost","root","password");  
// localhost refers to host address
// root refers to the database username
// password refers to database password

$db = mysql_select_db("Sample",$connect);
// $connect directs to the server connection
// Here Sample refers to our database name

// to check our server connection

if($connect)
{
    echo "Server connected successfully";
}
else
{
    echo "Problem in server connection";
}

// to check our database connection

if($db)
{
    echo "Database connected successfully";
}
else
{
    echo "Error in Database connection";
}

?>
  



Just run this code in browser localhost/connection/database.php

You can remove this if conditions, if it works successfully.



It is cool right ?