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.

strcmp – String Compare

Here are some of string comparation php functions:

  • strcmp($s1, $s2) – returns 0 if $s1 and $s2 are equal, a negativ value (-1) if $s1< $s2 and positive value (1) if $s1>$s2. The string comparation is case-sensitive, character by character, comparing character’s codes. Uppercase chars are considered smaller than minuscule chars. Numbers are considered smaller than letters.
  • strcasecmp($s1, $s2) – idem, but the string compare is case-insensitive
  • strncmp($s1, $s2, $nr) – like strcmp() but it compare only the first $nr chars of the 2 strings.
  • strncasecmp($s1, $s2, $nr) – idem, case-insensitive
  • strnatcmp($s1, $s2) – natural comparation, case-sensitive of the 2 strings (like natsort())
  • strnatcasecmp($s1, $s2) – idem, but case-insensitive

(more…)

strpos – Find the Position of a String

Finding the position of a string or substring can be made with this php functions:

  • strpos($string, $seached_string [,$offset]) – returns position of the first occurrence of $searched_string inside $string or FALSE if $searched_string is not found. If $offset is specified, the seach will be made starting with $offset position.
  • stripos($string, $seached_string [,$offset]) – idem, but the search is case-insensitive
  • strrpos($string, $seached_string [,$offset]) – returns the position of the last occurrence of $searched_string inside $string, or FALSE
  • strripos($string, $seached_string [,$offset]) – idem with strrpos but is case-insensitive

(more…)

PHP sef url function

This is another PHP sef function, much better to use in case you want to get remove characters that are not a-z, 0-9, dash, underscore or space, then remove all leading and trailing spaces, change all dashes, underscores and spaces to dashes and return the modified string in lower case.
(more…)

php echo and print differences

There are some differences between echo and print in php, echo and print are not functions.
First, you can pass more than one parameter to echo, print doesn’t allow more than one parameter.
Example:

  1. <?php
  2. $a = 5;
  3. $b = 6;
  4.  
  5. echo $a; // will output 5
  6. echo $a, $6; // outputs 56
  7.  
  8. print $a; //will output 5
  9. print $a, $b; // error, print doesn't allow more than one parameter
  10. ?>

Now, let’s see something interesting…

  1. <?php
  2. $a = 5;
  3. echo " \$a is:" . print $a; // will output 5 $a is 1
  4. ?>

First, you use \ (backslash) character to treat $a as a string not variable. Then, this is important, the code will be executed in this manner: first is executed print then echo. You may ask but why it will output “1″… Let’s see the next example:
Remember that print($arg) will return 1!

  1. <?php
  2. $a = 5;
  3. echo (print($a)); // will output 51
  4. ?>

As you can see, it will output 51 because php print is executed first (5) and with echo returns 1.

Page 1 of 3123»