Monday, April 30, 2012

PHP Introduction to Form Tutorial


Introduction to Form
The <input type="text"> tag is a text box, while <input type="submit"> creates a submit button for sending web page back to the web server.
Example: 1
index.php
<html>
    <head>
        <title></title>
    </head>
    <body>
        <form action ="loginTest.php" method="post">
            Name: <input type ="text" name="name"/>
            <input type="submit"/>
        </form>
        
    </body>
</html>
Output
Name: 
When we click submit button, nothing happens. Because in our loginTest.php is a blank or not exist yet that does not give any actions or directions or anything.

Example: 2
index.php
<html>
    <head>
       <title></title>
    </head>
    <body>
        <form action ="loginTest.php" method="post">
            Name: <input type ="text" name="name"/>
            <input type="submit"/>
        </form>
        
    </body>
</html>

loginTest.php
<html>
    <body>

<?php
   echo $_POST["name"];
?>
    </body>
</html>
Output
Name: 

Whenever we click submit button, control goes to the loginTest.php.
$_POST indicates that getting the information from a form and not a normal variable.
If you don’t write method="post", then it looks for a variable on the same page. i.e., by making $_POST, is going to look variable on another page.
echo $_POST["name"] says, we want to echo something from a form, whatever the input for the name variable.

PHP Basic Math Function Tutorial


Basic Math Function
PHP is pretty much like a big calculator.
You can do basic math function right in your variable. A variable equal to whatever equations you have and then displays the output on the screen. Bunch of stuff you can do, like addition, subtraction, multiply, division, remainder, etc.
Example: 1
        <?php
        $anything = 10;
        echo $anything;
        ?>
Output
10

Addition
Example: 2
        <?php
       $anything = 10+500;
       echo $anything;
        ?>
Output
510

Subtraction
Example: 3
        <?php
       $anything = 10-500;
       echo $anything;
        ?>
Output
-490

Adding two variables
Example: 3
        <?php
       $anything = 55;
       $something = 45;
       echo $anything + $something;
        ?>
Output
100

PHP strpos Function Tutorial


strpos Function
1. strpos function allows to find data in a particular string.
2. If a match is found in the string, strpos function will return the position of the first match. If no match is found, it will return FALSE.

Example: 1
<?php
   echo strpos("Hello world!","world");
?>
Output
6
The position of the data world in our string is position 6. 
Because, strops starts count from zero.


Example: 2
<?php
        $string = 'I will develop my wisdom and intuition. I will realize my infinite potential fully.';
        $find= 'wisdom';
       echo strpos($string, $find);
?>
Output
18 

PHP Include Function Tutorial


Include Function
Let's say we want to put hundred different web pages. Bunch of time, going to webpage to webpage and updating each one, one by one, with include function.
menu.php
<html>
     <head>
    </head>
 <body>

      <ul>
     <li>Home </li>
     <li>HTML</li>
     <li>PHP </li>
     </ul>

</body>
</html>
Output
  • Home
  • HTML
  • PHP

You need very time to display the above output in your every web page.
Just use include key word and file name that you attach.
first.php
<?php
include 'menu.php';
<p> This is first page <p/>
?>

second.php
<?php
include 'menu.php';
<p> This is second page <p/>
?>

You need not open menu.php file.
When you open first.php file and second.php file, menu.php file will automatically open each time. Need not require write extra code for menu. Include acts like function call.

PHP Trim Function Tutorial


Trim Function
Syntax:
trim(string,charlist)

The trim() function takes two parameters.
The trim() function strips whitespaces or other characters from the both sides i.e. beginning and end of a string.
If the second argument, charlist is not specified, trim() function strips all whitespace (" "), new line "\n", tab ("\t"), carriage return ("\r") by default. 
Example: 1
        <?php
       $str = '&nbsp;This is a test.&nbsp;';
       $str_trimed = trim($str);
       echo $str_trimed;
       echo $str;
        ?>
Output
 This is a test.  This is a test. 

Example: 2
       <?php
       $str = '?My workload is increasing. I am learning to prioritize.?';
       $str_trimed = trim($str, "?");
       echo $str;
       echo "<br>";
       echo $str_trimed;
       ?>
Output
?My workload is increasing. I am learning to prioritize.?
My workload is increasing. I am learning to prioritize.

Notice that '?' is trimmed both sides from string.

PHP strrev Function Tutorial


strrev Function
Syntax:strrev(string)
The strrev() function is used to to reverse a string.
Example
        <?php
       $str = 'This is a test';
       $str_reversed = strrev($str);
       echo $str_reversed;
        ?>
Output
tset a si sihT 

PHP strlen Function Tutorial


strlen Function
The strlen function is used to return the length of a string. 

Syntax
strlen(string)
Example
        <?php
       $str = 'I will dress suitably according to my environment.';
       $str_length = strlen($str);
       echo $str_length;
        ?>
Output
50

PHP str shuffle Function Tutorial


str shuffle Function
Syntax:
str_shuffle(string)

The str_shuffle() function randomly shuffles all the characters of a string.
The str_shuffle() function is mainly used for to to generate a random password or produce random characters for CAPTCHA validation image.
Each time this function executes and it will randomly re-shuffle the string.
Example: 1
        <?php
        $string = 'This is a test';
        $x=str_shuffle($string);
        echo $x;
        ?>
Output
aih istT sest

Random character generator.
Example: 2
        <?php
        $string = 'abcdefghijklmnopqrstwxyz0123456789';
        $x=str_shuffle($string);
        $y = substr($x,0, 5);
        echo $y;
        ?>
Output
g1c3w

Example: 3
        <?php
       $string = 'abcdefghijklmnopqrstwxyz0123456789';
       $x=str_shuffle($string);
       $y = substr($x,0, strlen($string)/2);
       echo $y;
        ?>
Output
57zo04xp3hykcl1ei 

See a password generator program below the link:
Password Generator Program 

PHP Explode Function Tutorial


Explode Function
syntax:
$array_name = explode($delimiter, $string);
The explode() function is used to split up strings based on the specified delimiter.
It is similar to the split function in Javascript.
Once the string is split or broken up, the pieces are stored in an array and they are numbered in order, starting from 0.
Example: 1
        <?php
        $str = "I am responsible for my own health.";
        print_r (explode(" ",$str));
        ?>
Another way:
        <?php
        $str = "I am responsible for my own health.";
        $parts = (explode(" ",$str));
        print_r ($parts);
        ?>
Output
Array ( [0] => I [1] => am [2] => responsible [3] => for [4] => my [5] => own [6] => health. ) 

We can apply explode function on date or phone number.
Let's see an example.
Example: 2
        <?php
        $D_M_Y = date("d/m/Y");
        $parts = (explode('/' ,$D_M_Y));
        print_r ($parts);
        ?>
Output
Array ( [0] => 30 [1] => 04 [2] => 2012 ) 

Notice that today's date is broken.

Sunday, April 29, 2012

PHP gatedate Function Tutorial


gatedate Function
The getdate() function returns an array that contains date ten elements with relevant information needed when formatting a date string:
      [seconds] - seconds
      [minutes] - minutes
      [hours] - hours
      [mday] - day of the month (numeric) 
      [wday] - day of the week (numeric) 
      [mon] - month 
      [year] - year
      [yday] - day of the year (numeric) 
      [weekday] - name of the weekday
      [month] - name of the month
Syntax
getdate(timestamp)
timestamp is optional.

Example: 1
<?php
print_r(getdate());
?>
Output
Array ( [seconds] => 51 [minutes] => 58 [hours] => 2 [mday] => 30 [wday] => 1 [mon] => 4 [year] => 2012 [yday] => 120 [weekday] => Monday [month] => April [0] => 1335769131 ) 

Example: 2
       <?php
       $today = getdate();
       $Month = $today['month'];
       $Day = $today['mday'];
       $Year = $today['year'];
       $Hour = $today['hours'];
       $Minutes = $today['minutes'];
       $Seconds = $today['seconds'];
       echo "Today's date is $Day $Month $Year"."<br>";
       echo "The time is now $Hour $Minutes $Seconds";
       ?>
Output
Today's date is 30 April 2012
The time is now 2 58 51 

PHP Date Function Tutorial


Date Function
To display in numeric format:
1. d, a lower case d, stands for date of the month( 1 to 31).
2. m, a lower case m, stands for the month( 1 to 12).
3. y, a lower case y, stands for the year(in two digits).

To display in text format:
1. D, a capital D, stands for day of the month in tex( Saturday, Sunday, etc. ).
2. M,a capital M, stands for the month in text( 1 to 12).
3. Y, Capital Y, stands for date of the year(in four digits).


To display in numeric format:
Example: 1
        <?php
        echo date("d/m/y");
        ?>
Output
30/04/12

You can change the format like period.
Example: 2
        <?php
        echo date("d.m.y");
        ?>
Output
30.04.12

You can also use minus sign.
Example: 3
        <?php
        echo date("d-m-y");
        ?>}
Output
30-04-12

To display in text format:
Example: 1
        <?php
        echo date("D/M/Y");
        ?>
Output
Mon/Apr/2012

You can change the format like period.
Example: 2
        <?php
        echo date("D.M.Y");
        ?>
Output
Mon.Apr.2012

You can also use minus sign.
Example: 3
        <?php
        echo date("D-M-Y");
        ?>}
Output
Mon-Apr-2012 

To display full date:
Example: 4
        <?php
        echo date("d D M Y");
        ?>}
Output
30 Mon Apr 2012 

The following example will output "Have a nice weekend!" if the current day is Sunday, otherwise it will output "Have a nice day!":
Example: 5
<?php
$d=date("D");
if ($d=="Sun")
  echo "Have a nice weekend!";
else
  echo "Have a nice day!";
?>
Output
Have a nice day!