Learn PHP programming - PHP tutorials

learn php tutorial

 Welcome to MysqlPhpTutorial.com, the weblog where we discuss about php programming and development by creating easy to learn php tutorials.
 Functions, sessions, arrays, classes and forms - a overwhelming amount of information you need to absorb in order to perform better as a coder.

 We believe that php examples presented on the site will boost your coding knowledges and help you develop performant, secure php applications.

Save PHP form in file

With php you can store data, save php form in text file such as notepad and even in microsoft word too.
The following php code will make you able to store form data in microsoft word file. It will create ms word file too that if file is not created before.

  1. < ?php
  2. $username = $_POST['username'];
  3. $fp = fopen("myfile.doc","a+") or exit("unable to open the file");
  4. if($fp != null)
  5. {
  6. fwrite($fp,$username);
  7. }
  8. fclose($fp);
  9.  
  10. ?>

(more…)

PHP substr() function

PHP substr() function will substract or returns the portion of string specified by the start and length parameters.
substr($string , int $start [, int $length ] )

  1. <?php
  2. $sub = substr("abcdef", -2);    // returns "ef", -2 represent starting from 2 character back
  3. $sub = substr("abcdef", -3, 1); // returns "d", -3 represent starting from 3 character back and 1 the length of the substraction
  4. echo substr('abcdef', 0, 3);  // returns "abc", starting from 0 to 3
  5.  
  6. // you can substract only one character
  7. $string = 'abcdef';
  8. echo $string[3]; // returns "d", the third character
  9. ?>

PHP list function with mysql_query

When we want to output something from a mysql database we select, query it then use the mysql_fetch_array function to return an array that corresponds to the fetched row.
Most of us are using $row['rowname'] to extract desired info from that row but we can make use of list() function:
Example using $row['rowname']:

  1. $select = "SELECT * FROM users ORDER BY id ASC";
  2. $query = mysql_query($select);
  3. while($row = mysql_fetch_array($query)) {
  4.  echo "{$row['id']} {$row['name']} {$row['pass']}";
  5. }

(more…)

PHP censor words function

This php function takes a list of words you define and censors them. In the example you have an array of banned words and a $replace variable that is used as a replacement of the censored words.
By using eregi_replace function we can check every word in the array and replace it with the desired, proper code.

  1. <?php
  2.  
  3. $banned = array("visit", "http://", "www.", ".com");
  4. $replace = '<span style="color:red; font-style: italic;">×××</span>';
  5.  
  6. function censor($content) {
  7.     global $banned, $replace;
  8.  foreach($banned as $words) {
  9.  $content= eregi_replace($words, $replace, $content);
  10.  }
  11. return $content;
  12. }
  13.  
  14. echo censor("Visit us at http://www.phpjunk.com");
  15.  //or if you store data into a variable:
  16. echo censor($data);
  17. ?>
Page 2 of 3123