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