, ,

Handling Forms in PHP with easy example

Hello friends, this is my 14th tutorial of PHP and Today I am sharing the concept of form processing in PHP. Forms are almost useeverywhere. Forms are the simple and convenient way ask your user to provide a certain set of data. Forms are very simple to design and implement in PHP. In PHP we can easily process the form data. Let me demonstrate this with an example.
Creating a simple form :
Form.html
<form action="process.php" method="post">
Username:<input type="text" name="username" value="">
Password:<input type="password" name="password" value="">
<input type="submit" name="submit" value="Submit">
</form>
Processing the data entered in the form by using a simple enough PHP script :
 Process.php
<?php  
//Fetching data using Post method
$username=$_POST['username'];
$password=$_POST['password'];
echo "{$username} : {$password}";

//Fetching data using Get method
$username=$_GET['username'];
$password=$_GET['password'];
echo "{$username} : {$password}";
?>
Publisher: Santosh Tewari - 09:15
, ,

Handling Sessions in PHP with simple example

Hello friends, this is my 13th tutorial of PHP and Today I am sharing the concept of using Sessions in PHP. Sessions are useful for storing and maintaining particular session information. In PHP sessions can be created and maintained very easily. Let us consider a simple example :
 <?php  
//Starting a session
session_start();

//Storing values to session
$_SESSION['first_name']='Santosh';
$_SESSION['last_name']='Tewari';

//Accessing values from session
$fname=$_SESSION['first_name'];
$sname=$_SESSION['last_name'];
echo $fname . " " . $sname;
?>
The session must be started at the very beginning of a php code block. $_SESSION[] is a global array that maintains information about all session objects. You can store and access values from session by using this global array.
Feel free to share your ideas in the comments below.
Publisher: Santosh Tewari - 09:10
, ,

URL Encoding in PHP with easy example

Hello friends, this is my 12th tutorial of PHP and Today I am sharing the concept of URL encoding in PHP. Every user needs data privacy so whenever you are passing data over URL you should encode it properly so that I can be saved from some beginning level threads. URL encoding is used for this purpose, with it's help you can encode the data that you are passing over the network. Let us consider the following example :
 <?php  
$list=" & go to second page";
?>
<a href="secondpage.php?id=1&name=<?php  echo urlencode("santosh&&"); ?>">Second Page</a>
<a href="secondpage.php?id=1&name=<?php  echo urlencode("santosh&&"); ?>"><?php  echo htmlspecialchars($list); ?></a>
As you can see in the above example we have encoded the data that we are passing through the URL.
Publisher: Santosh Tewari - 08:56
, ,

Database connectivity in PHP in 5 simple steps with simple example

Hello friends, welcome again, this is my 11th tutorial of PHP and Today I am sharing the concept of using database in PHP. Database is a vital portion of a programming language because this is where we store our data so it's really important that we have a clear idea about how to connect to database, how to fetch data from database, storing data to database and updating and deleting data from database.
In PHP we can connect to database by using five simple steps as demonstrated below :
 <?php  
//Step-1 : Create a database connection
$connection=mysql_connect("localhost","root","root");
if(!$connection) {
 die("Database Connection error" . mysql_error());
}
//Step-2 : Select a database to use
$db=mysql_select_db("widget_corp",$connection);
if(!$db) {
  die("Database Selection error" . mysql_error());
}
//Step 3 : Perform database Queury
$result=mysql_query("select * from subjects",$connection);
if(!$result) {
 die("No rows Fetch" . mysql_error());
}

//Step 4 : Use returned data
while($row=mysql_fetch_array($result)) {
 //echo $row[1]." ".$row[2];
 echo $row["menu_name"]." ".$row["position"];
}

//Step 5 : Close Connection 
mysql_close($connection);
?>
In the above code I have demonstrated 5 simple steps for connecting to database. Hope it will be useful for you. Feel free to post your ideas in the comments below.
Publisher: Santosh Tewari - 08:05
, ,

Using Cookies in PHP with easy example

Hello friends, this is my 10th tutorial of PHP and Today I am sharing the concept of using Cookies in PHP. Cookies are useful for storing small chunk of data at the client browser that you can use later for providing faster services to your user as you don't have to fetch that data from the server.
Creating cookies an saving it on client's browser :
 <?php  setcookie('test',200,time()+(60*60)); ?>
As you can see, I have passed three arguments while creating the cookie, the first argument specifies the name of the cookie, second argument specifies the value for the cookie and the third argument specifies the default expiration time of the cookie.
Fetching cookies that are saved on client's browser :
 <?php  
$var1=0;
if(isset($_COOKIE['test'])) {
 $var1=$_COOKIE['test'];
}
echo $var1;
?>
Remember to check if the cookie exist or not before fetching it as cookies can automatically get deleted once the it's expiration time is over.
Publisher: Santosh Tewari - 08:55
, ,

Using Functions in PHP with easy example

Hello friends, this is my 9th tutorial of PHP and Today I am sharing the concept of using functions in PHP. Functions are block of codes that performs specific operations. Functions are useful as they help to make a code modular and more structure.
Creating function with no argument and no return type :
 <?php 
function show() {
 echo "Show is called";
}
show();
?>
Creating function with argument but no return type :
 <?php 
function sum($a,$b) {
 $c=$a+$b;
 echo "Sum is : {$c} ";
}
sum(10,20);
?>
Creating function with argument and return type :
 <?php 
echo mul(2,3);
function mul($a,$b) {
 $c=$a*$b;
 return $c;
}
?>
Creating function that returns multiple values :
 <?php 
function sum_diff($a,$b) {
 $c=$a+$b;
 $d=$a-$b;
 $result=array($c,$d);
 return $result;
}
$res=sum_diff(50,30);
echo "Sum : {$res[0]} ";
echo "Difference : {$res[1]}";
?>
Using global variables inside a function :
 <?php 
$b="Outside";
function glo_change() {
  global $b;
  $b="Local";
}
glo_change();
echo $b;
?>
Using default parameter inside a function :
 <?php 
function paint($color="red") {
  echo "Color of the room is {$color}";
}
paint("blue");
paint();
?>
Publisher: Santosh Tewari - 07:59
, ,

String manipulation in PHP with easy example

Hello friends, this is my 8th tutorial of PHP. Today I am sharing the concept of String manipulation in PHP. Strings are almost used everywhere, so it is important that you have a good command over strings so that you can manipulate them easily to work accordingly.
Let us start with initializing a string :
 <?php  
$firststring="The quick brown fox";
$secondstring=" jumped over the lazy dog.";

//Assigning one string to another
$thirdstring = $firststring;
$thirdstring .= $secondstring;
echo $thirdstring;
?>
Here is a list of certain methods that may come handy while manipulating strings :
 Lowercase :  <?php  echo strtolower($thirdstring); ?>
Uppercase : <?php  echo strtoupper($thirdstring); ?>
Uppercase first letter : <?php  echo ucfirst($thirdstring); ?>
Uppercase words : <?php  echo ucwords($thirdstring); ?>

Length : <?php  echo strlen($thirdstring); ?>
Trim : <?php  echo $fourthstring=$firststring . trim($secondstring); ?>
Find : <?php  echo strstr($thirdstring,"brown"); ?>
Replace by String : <?php  echo str_replace("quick","so quick",$thirdstring); ?>
Publisher: Santosh Tewari - 09:26
, ,

Using pointers in PHP with easy example

Hello friends, this is my 7th tutorial of PHP and Today I am sharing the concept of using pointers in PHP. Pointers always come handy while manipulating data in a programming language. In this blog post I will share a simple example of using pointers in PHP.
In the first example I will explain how you can use pointers to access array elements using pointers :
 <?php  
$age=array(4,56,76,34,54);
echo "1 . " . current($age); // Points to the first element in the array.
next($age);
echo "2 . " . current($age); // Points to the second element in the array
reset($age);
echo "3 . " . current($age); //Pointer is reset and points to the first element again.
?>
As you can see in the above example, I have used three different methods named current(), next() and reset() for accessing the array elements.
Let us consider another example where we will try to access all the elements in an array by using the above methods and a while loop :
 <?php  
while($var=current($age)) {
 echo $var;
 next($age);
}
?>
Publisher: Santosh Tewari - 07:32
, ,

Using Array in PHP with easy example

Hello friends, this is my 6th tutorial of PHP and Today I am sharing the concept of using arrays in PHP. As we all know that array is a collection of homogeneous elements that are stored in contiguous memory locations. In PHP, the syntax used for creating arrays is slightly different but still is very simple.
Creating an array with homogeneous elements :
 <?php  $array1=array(10,20,30,40,50); ?>
Printing a specific element from an array :
 <?php  echo $array1[1]; ?>
Creating an array with heterogeneous elements :
 <?php  $array2=array(6,"santosh","rahul",array("x","y","z")); ?>
Accessing values from the above multidimensional heterogeneous array :
 <?php  
echo $array2[3]; // Accessing complete array inside another array.
echo $array2[3][1]; // Accessing specific element of an array inside another array.
echo $array2[1]; // Accessing a specific element from the array
?>
Changing an array element with anther value :
 <?php  $array2[3]="cat"; ?>
Creating an array with key-value pair :
 <?php  $array3=array("fname"=>"Jhon","lname"=>"Smith" ); ?>
Printing a specific element from an array created using key-value pair :
 <?php  echo $array3["fname"]; ?>
Printing entire array :
 <?php  print_r($array2); ?>
Here are some common methods for manipulating arrays in PHP that may come handy while working with arrays :
Creating array : <?php  $array1=array(10,40,30,20,50); ?>
Count : <?php  echo count($array1); ?>
Max : <?php  echo max($array1); ?>
Min : <?php  echo min($array1); ?>
Sort : <?php  echo sort($array1); print_r($array1); ?>
Reverse sort : <?php  echo rsort($array1); print_r($array1); ?>
Implode : <?php  echo $str1 = implode(" * ",$array1); ?>
Explode : <?php  echo print_r(explode(" * ",$str1)); ?>
In array : <?php  echo in_array(30,$array1); ?>
Publisher: Santosh Tewari - 12:19
, ,

Using Constants in PHP with easy example

Hello friends, this is my fifth tutorial of PHP and Today I am sharing the concept of using Constants in PHP. As we all know that constants are the values that remains fixed during the entire course of a program= and thus are really helpful for programmers for storing values that they can anywhere in the code. In PHP, using constant is very easy. Let us consider the example shown below :
 <?php 
$max_width=890;
define("MAX_WIDTH",890);
echo MAX_WIDTH;
?>
As you can see in the above code, I have used the define() method for creating a new constant named 'MAX_WIDTH'.
Publisher: Santosh Tewari - 12:19
, ,

Type Casting in PHP with easy example

Hello friends, here I am back again with the second basic tutorial of PHP and Today I am sharing the basics of type casting in PHP . If simply explained, type casting is a feature of any programming language to convert one data type to another data type value according to programmers convenience or compiler's. If the conversion is done by the compiler then it is called Implicit type casting and if it is done by the programmer then it is called Explicit type casting.
Consider the following code snippet of code :
 <?php 
$var1="2";
$var2=$var1+4;
echo $var2;
?>
In the above code the implicit type casting is explained. As you can see in the code that the $var1 is initialized with a string value which is then added with a integer value the resulting integer value is stored in $var2. Next, let us take a look on explicit type conversion :
 <?php 
echo gettype($var1);
echo gettype($var2);
settype($var2,"string");
$var3=(int)$var2;
echo gettype($var3);
?>
As you can see in the above code, first I have found the type of a php variable using gettype() and then set the type of a variable using settype(). I have also demonstrated that how you can change data type of a variable by explicitly specifying the datatype as in the case of $var3 where I have used the int datatype. Below is a list of method that may come handy while you are dealing with type casting :
Is Boolean :   <?php   echo is_bool($var1);   ?>
Is Array :   <?php    echo is_array($var1);   ?>
Is Float :   <?php   echo is_float($var1);   ?>
Is Int :   <?php    echo is_int($var1);   ?> 
Is Null :   <?php    echo is_null($var1);   ?>
Is Numeric :   <?php    echo is_numeric($var1);   ?>
Is String :   <?php    echo is_string($var1);   ?>
Publisher: Santosh Tewari - 12:19
, ,

Using loops in PHP with easy example

Hello friends, this is my fourth tutorial of PHP and Today I am sharing the concept of using loops in PHP. Loops provides the feature of iterating through the same block desired number of times. There are different types of loops that can be used in PHP. So without wasting any time lets start.
Using while loop in PHP :
 <?php 
$c=1;
while($c<=10) {
 echo $c . ", ";
 $c+=1;
}
?>
Using for loop in PHP :
 <?php 
for($c=1;$c<=10;$c++) {
 echo $c . ", ";
}
?>
Using for-each loop in PHP :
 <?php 
$arr1=array(10,20,30,40,50);
foreach($arr1 as $var1) {
 echo $var1 . " ,";
}
//Another approach
foreach($arr1 as $pos=>$val) {
 echo $pos . " Contains " . $val . "
"; } ?>
Using for loop with continue and break statement in PHP :
 <?php 
for($c=1;$c<=10;$c++) {
 if($c==5)continue;
 if($c==8)break;
 echo $c . ", ";
}
?>
Publisher: Santosh Tewari - 12:19
, ,

Using logical operators in PHP with easy example

Hello friends, this is my third tutorial of PHP and Today I am sharing the concept of using logical operators in PHP. Conditional operators are one of the simple and widely used feature of a programming language. So without wasting any time lets start.
Using if-else structure in PHP :
 <?php 
$a=10;
$b=20;
if($a>$b) {
 echo " A is Greater";
}
elseif($a<$b) {
 echo " A is lesser";
}
else {
 echo "A and B are equal";
}
?>
Using if-else structure with multiple conditions in PHP :
 <?php 
$c=30;
$d=40;
//if(($a<$c)AND($b<$d))
if(($a<$c)&&($b<$d)) {
 echo "A and B are larger";
}
if(isset($d)) //Checks if a variable is initialized or not
$d=100;
echo $d;
unset($d);
?>
Using switch-case in PHP :
 <?php 
$var1=2;
switch($var1)
{
 case 1 : echo "var1 is 1";
         break;
 case 2 : echo "var1 is 2";
         break;
 case 3 : echo "var1 is 3";
         break;
 default : echo "var1 is unknown";
}
?>
Publisher: Santosh Tewari - 11:06

Find intermediate coordinates between two points of a Map programmatically in iOS using Objective-C

Here is a sample code to find the co-ordinates of a location from the location name:
Code:
-(NSMutableArray *)decodePolyLine: (NSMutableString *)encoded {
    [encoded replaceOccurrencesOfString:@"\\\\" withString:@"\\"
             options:NSLiteralSearch
             range:NSMakeRange(0, [encoded length])];
    NSInteger len = [encoded length];
    NSInteger index = 0;
    NSMutableArray *array = [[NSMutableArray alloc] init];
    NSInteger lat=0;
    NSInteger lng=0;
    while (index < len) {
        NSInteger b;
        NSInteger shift = 0;
        NSInteger result = 0;
        do {
            b = [encoded characterAtIndex:index++] - 63;
            result |= (b & 0x1f) << shift;
            shift += 5;
        } while (b >= 0x20);
        NSInteger dlat = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lat += dlat;
        shift = 0;
        result = 0;
        do {
           b = [encoded characterAtIndex:index++] - 63;
           result |= (b & 0x1f) << shift;
           shift += 5;
        } while (b >= 0x20);
        NSInteger dlng = ((result & 1) ? ~(result >> 1) : (result >> 1));
        lng += dlng;
        NSNumber *latitude = [[NSNumber alloc] initWithFloat:lat * 1e-5];
        NSNumber *longitude = [[NSNumber alloc] initWithFloat:lng * 1e-5] ;
        printf("[%f,", [latitude doubleValue]);
        printf("%f]", [longitude doubleValue]);
        CLLocation *loc = [[CLLocation alloc] initWithLatitude:[latitude floatValue] longitude:[longitude floatValue]];
        [array addObject:loc];
    } 
    return array;
}
-(NSArray*) calculateRoutesFrom:(CLLocationCoordinate2D) f to: (CLLocationCoordinate2D) t {
    NSString* saddr = [NSString stringWithFormat:@"%f,%f", f.latitude, f.longitude];
    NSString* daddr = [NSString stringWithFormat:@"%f,%f", t.latitude, t.longitude];
    NSString* apiUrlStr = [NSString stringWithFormat:@"http://maps.google.com/maps?output=dragdir&saddr=%@&daddr=%@",  saddr, daddr];
    NSURL* apiUrl = [NSURL URLWithString:apiUrlStr];
    NSLog(@"api url: %@", apiUrl);
    NSString *apiResponse = [NSString stringWithContentsOfURL:apiUrl];
    NSString* encodedPoints = [apiResponse stringByMatching:@"points:\\\"([^\\\"]*)\\\"" capture:1L];
   return [self decodePolyLine:[encodedPoints mutableCopy]];
}
Publisher: Santosh Tewari - 09:54
, ,

Number format in PHP with easy example

Hello friends, this is my first tutorial of PHP and I am starting with the Numbers. As for any programming language the Numbers are the basics so I am thinking that starting PHP is better with numbers. Today I will discuss Number format in PHP with easy example. I will discuss with you the various ways in which we can use numbers and also give you examples by using which you can easily manipulates with Numbers. First thing to take into consideration is that every PHP code is written within:
 <?php  Your Code Comes Here ?>

For Working with Numbers we need variables. So let's create some variables in PHP :
 <?php 
$var1=3;
$var2=4;
?>
Now lets look at some basic operations that we can perform with numbers in PHP :
Arithmetic Operation :   <?php    echo ((1+3+$var1)*$var2)/2-5;   ?>
+= Opeation :   <?php   $var2+=4;   ?>
-= Operation :   <?php    $var2-=4;   ?>
*=& Operation :   <?php    $var2*=4;   ?>
/= Operation :   <?php    $var2/=4;   ?> 
Increment:   <?php    $var2++;   ?>
Decrement:   <?php    $var2--;   ?>
Now Lets work with some floating point numbers. I will show you how you can perform some basic operations with Floating point numbers in PHP :
 Declaring Floating point Variable:
<?php 
$var3=3.14;
echo 4/3; 
?>
Printing Floating point Variable:   <?php    echo $var3;    ?>
Round:   <?php    echo round($var3);    ?>
Ceiling Value:   <?php    echo ceil($var3);    ?>
Floor Value:   <?php    echo floor($var3);    ?>
Absolute Value:   <?php    echo abs(0-200);    ?>
Power:   <?php    echo pow($var3,2);    ?>
Square Root:   <?php    echo sqrt($var3);    ?>
Random Value:   <?php    echo rand();    ?>
Random Min-Max:   <?php    echo rand(1,100);    ?>
Modulus:  <?php    echo fmod(20,7);    ?>
Publisher: Santosh Tewari - 05:33
 

 

Our Leading Clients

Awesome people who trust us