Thursday, March 22, 2012

Functions


                //Basic founctions
 
 function name() {
     echo 'Waqas</br>';
}
 name();

                 //another example
function table() {
      echo 'Name:</br>Roll #:</br>';
  }
table();

                  //functions with arguments
$n1 =3;
$n2 =4;


function add($numb1, $numb2) {
        echo $numb1 + $numb2.'</br>';
}
add($n1, $n2);

                    //anther example

function ddy($day, $date, $year) {
        echo $day.' '.$date.' '.$year.'</br>';
}
ddy('Monday', 19, 2012);


                      //function with return value
function add2($num1, $num2) {
       $result =$num1 + $num2;
       return $result; 
}

function multiply($num1, $num2) {
       $result = $num1 * $num2;
    return  $result;
}

$sum = multiply(add2(2,2), add2(2,2));
echo $sum .'</br>';

                     //Global Variables and Functions
$ip = $_SERVER['REMOTE_ADDR'];

function ech_ip() {
     global $ip;
    $line = 'Your ip is: '.$ip;
echo $line.'</br>';
}
@ech_ip();


                           //random function
//rand(lower limit , upper limit);

 $rand = rand();
$max_limit = getrandmax();
echo $rand.' / '. $max_limit.'</br>';


               //header function                                            //out put buffer
 //header('Location:'. web addr);             //ob_start(); & ob_end_flush();
$redirect_page = 'https://www.google.com';
$redirect = false;

if ($redirect==true) {
     header('Location: '.$redirect_page);
}


                       //include and require functions & include_once & require_once functions
       page1                                                 page2

<h2>My pap</h2>                            <?php
<?php                                                  require 'hdr.php';
                                                             require_once 'hdr.php';
                                                            ?>
?>

                                                               



                                                                           






String function



                              //String Functions
                        //str_word_count();                  //the 2nd argument z optional

$str = 'my name is waqas.';
$str_word_count = str_word_count($str, 0);
echo 'the string has <b>'.$str_word_count .'</b> words.</br>';

                  //printing in an array
               //str_word_count($str1, 1};
$str1 = 'my name is waqas.';
$an_array = str_word_count($str1, 1);
 print_r($an_array);
echo '</br>';
                  //pirnting in an associative array
               //str_word_count($str2, 2);
$str2 = 'my name is waqas .';
$an_associative_array = str_word_count($str2, 2, '.');
print_r($an_associative_array);

                  //string shuffle
//str_shuffle();
$str3 = 'my name is waqas.';
$shuffle = str_shuffle($str3);
echo $shuffle.'</br>';

                    //sub string
//substr($str4, argument);
$str4= 'my name is waqas';
$sub_string = substr($str4, 4);
  echo $sub_string.'</br>';                

                     //string lenth
                    //strlen();
$str5 ='my name is waqas.';

$str_lenth =strlen($str5);
echo 'string lenth '.$str_lenth.'</br>';

                    //string lenth with for loop
$string_w = 'waqa s';
$string_l = strlen($string_w);
    for ($x=1; $x<=$string_l; $x++) {
     echo $x.' ';
    }

  //A program ussing string shuffle, sub string & string lenth
$str6 = 'my name is waqas';
$shuf = str_shuffle($str6);
$half = substr($shuf, 0, strlen($str6)/4);              //substr($shuf, 0, 10);
echo $half.'</br>';

                   //string revers
  //strrev();
$str7 = 'my name is waqas';
$str_revers = strrev($str7);
echo $str_revers.'</br>';

                   //similar text
   //similar_text();
$str_a = 'my name is waqas..';
$str_b = 'my last name is khan.';
similar_text ($str_a, $str_b, $rsut);
echo 'The similarty is:<b> '.$rsut,' %</b></br>';

                  //String Functions: Upper / Lower Case Conversion
 //strtoupper();, &strtolower();
$upper = 'upper case';
$string_uc = strtoupper($upper);
echo '<b>'.$string_uc.'</b></br>';

$lower = 'LOWER CASE';
$string_lc = strtolower($lower);
echo '<i>'.$string_lc.'</i></br>';

  //string trim
//trim();, ltrim();, &rtrim();

                   //add slashes
  //addslashes(); & stripslashes();
$code = 'my name is<img src="image.jpg"> waqas';
$add_slashes = htmlentities(addslashes($code));
echo stripslashes($add_slashes);


                    //Expression matchion
//preg_match('/word/', $a1);
$a1 = 'find the word';

if(preg_match('/word/', $a1)) {
      echo 'Match found</br>';
} else {
echo 'Match not found</br>';
}

                //preg_match: another program with function
function has_space($a2) {
  if(preg_match('/ /', $a2)) {
    return true;
} else {
return false;
}
}
if (has_space('mynameiswaqas')) {
    echo '<b>line has space</b></br>';
} else {
echo '<b>line has no space</b></br>';
}

                      //string positiion function
 //strpos($b1, $find, $offset);        //offset is optional
$expl = 'my name is waqas';
$loc_pos= strpos($expl, 'is');
 echo $loc_pos.'</br>';
 
                      //string position with while loop
$offset = 0;
$find ='p';
$find_len = strlen($find);

$b1 = 'This is my php file';
  while($find_pos = strpos($b1, $find, $offset)) {
        echo '<b>'.$find.' </b>find at: '.$find_pos.'</br>';
$offset = $find_pos + $find_len;
}
 
                    //String Functions: Replacing Part of a String
//substr_replace($c1, replace, start from, chr leth);
$c1 = 'my name is waqas & my last name is khan';
$string_new = substr_replace($c1, 'and', 17, 1);
echo $string_new.'</br>';
 
                    //String Functions: Replacing Predefined Part of a String
//str_replace(looking for, replace with, string);
$find_w = array('1946', 'bad', 'no');
$replace_w = array('1947', 'good', 'on');

$a2 = 'Pakistan came in to being no 1946 and that is bad';
$str11 = str_replace($find_w, $replace_w, $a2);
echo $str11;

Wednesday, March 21, 2012

Find and Replace Application in php using strlen();, strpos();, & substr_replace(); functions


<?php
$offset = 0;
if(isset($_POST['text'])&& isset($_POST['find'])&& isset($_POST['replace'])) {
 $text = $_POST['text'];
 $find = $_POST['find'];
 $replace = $_POST['replace'];

  $find_len = strlen($find);

       if(!empty($text) && !empty($find) && !empty($replace)) {
 
      while($strpos = strpos($text, $find, $offset)) {
   $offset = $strpos + $find_len;
$text = substr_replace($text, $replace, $strpos, $find_len);
}
        echo $text;
 
  } else {
  echo 'please fill in all fields';
  }
}

?>
<form action="app3.php" method="post">
<textarea name="text" rows="7" cols="30"></textarea></br></br>
Search for:</br>
<input type="text" name="find"></br></br>
Replace with:</br>
<input type="text" name="replace"></br></br>
<input type="submit" value="Find & Replace">
</form>

Word Censoring app in php using str_ireplace(); function


<?php

$find = array('waqas', 'ali', 'wahab');
$replace = array('w***', 'a**', '****');


if(isset($_POST['user_input']) or !empty($_POST['user_name'])) {
 $user_input = $_POST['user_input'];
 $user_input_2 = str_ireplace($find, $replace, $user_input);
 echo $user_input_2;
 }


?>
<hr>
<form action="app2.php" method="post">
<textarea name="user_input" cols="30" rows="6"></textarea></br></br>
<input type="submit" value="submit">
</form>

Php app using strtolwer(); function



<?php

 if (isset($_GET['user_name']) or !empty($_GET['user_name'])) {
   $user_name =$_GET['user_name'];
   $user_name_lc = strtolower($user_name);
   
   if($user_name_lc=='waqas') {
   echo '<strong>Well come!</strong> '.$user_name;
   }
   
}

?>
<form action="app1.php" method="get">
Name: <input type="text" name="user_name"></br>
<input type="submit" value="submib">
</form>







Wednesday, March 14, 2012

php basic


echo 'waqas</br>';
           
   //variables
$txt = '<b>waqas khan</b>';
$numb = 134;
echo $txt.' '.'<u>'.$numb .'</u></br>';
           

            //if/ if else ststment
$a = 2;
$b = 3;
                 //if statment
     if ($a!=$b) {
 echo 'true </br>';
}
       
        //if else statment
$a1 = 3;
$b1 = 10;
      if($b1<=$a1) {
    echo 'true </br>';
    } else {
echo 'false </br>';
}
           //if/else if statment

 $a2 = 8;
         if($a2==10) {
echo 'equal to 10';
} else if ($a2==5) {
            echo 'equal to 5';
           } else  {
            echo 'not equal! </br>';
        }
                 //assignment operetors
     // -=, +=, %=, *=
$b2 = 5;
$b2 += 3;
echo $b2.'</br>';

 $nam = '<b>hammad</b>';
$nam .= ' ali';
  echo $nam.'</br>';
//comparision operators
  // ==, >, <, >=, <=, !=, <>

if(1<>2) {
echo 'true </br>';
}
if(1!=2) {
 echo 'true </br>';

                  
   //arithmetic operators
 // +, -, *, /, %, ++, --
 
$sum = 3+4;  
echo $sum.'</br>';


$a3 = 10;
$b3 = 2;
$c1 = 4;
$result = ($a3 - $b3) / $c1;  
echo $result.'</br>';  
 
$c2 = 4;
$c2++;
echo $c2.'</br>';  
 
//logical operators
                      // &&, or, !,


$c3 = 5; 

if ($c3>=4 && $c3<=4) {
      echo 'true';
 } else {
 echo 'false </br>';
 }


$a4 = 4;
     if($a4==4 or $a4==3) {
echo 'true </br>'; 
} else {
echo 'false';
}


$b4 = 5;
      if  (!($b4==5) && ($b4==5)) {
echo 'true';
        } else {
echo 'false </br>';
}


           //triple equals
             // ===,
$string = '1';
$integer = 1;
          if($string===$integer) {
 echo 'equal';
 } else {
 echo 'not equal </br>';
 }


                  //while loop
$c4 = 1;
while ($c4<=10) {
  echo $c4.' WAQAS</br>';
  $c4++;
  }


$a5 = 10;
      while ($a5>=1) {
 echo $a5.' KHAN</br>';
 $a5--;
 }


                    //do while loop
$b5 =1;
      do {
 echo ' Show';
 $b5++;
 } while ($b5<=3)

do {
echo 'Show</br>';

while (0)


//for loop
for($c5=10; $c5>=1; $c5--) {
echo $c5.' ';
}

for($a6=1; $a6<=10; $a6++) {
echo $a6.' ';
}

//switch statement
$b6 = 1;
switch($b6) {
case 1:
echo 'One</br>';
break;

case 2:
echo 'Two';
break;

case 3:
echo 'Three';
break;

default:
echo 'number not found</br>';
break;
}

//Another switch statement
$c5 = 'black';
switch($c5) {
case 'red':
case 'yellow':
case 'green':
echo 'Traffic ligth color</br>';
break;

default:
echo 'Not a traffic ligth color</br>';
break;
}
 
//Array $a7 = array('waqas', 'wahab', 'waleed'); $a7[3] = 'hammad'; print_r($a7); //echo $a7[1].'</br>'; //Associative array $b7 = array('waqas'=>26, 'wahab'=>18, 'waleed'=>22); print_r($b7); //echo $b7['wahab']; //Multi-dimensional arrays $c7 = array('friends'=> array('waleed', 'hammad', 'ali'), 'gfz'=> array('popo', 'mam', 'no one')); echo $c7['gfz'][0]; //for each statement $a8 = array('friends'=> array('waleed', 'hammad', 'ali'), 'gfz'=> array('popo', 'mam', 'no one')); foreach($a8 as $element => $inner_array) { echo '<u><b>'.$element.'</u></b></br>'; foreach($inner_array as $items) { echo $items.'</br>'; } }  
//die and exit functions
//keywords: "die();" & "exit();"

@mysql_connect('localhost'.'root'.'') or die('Not connected');
echo 'Connected!';