<?php $_SERVER[‘PHP_SELF’]; ?>
If you use superglobal $_SERVER with PHP_SELF in form’s action=”<?php $_SERVER[‘PHP_SELF’]; ?>”, then it will keep you on the same page.
・include()
This is a very useful function to include header.php and footer.php of some other files to make it easier to maintain. Sometimes when used with conditional logic then different header or footer can be served to the user. E.g logged in AND logged out user can have different headers served to them when function include(); is used with conditional logic.
・define()
Used to DEFINE constants. In code CONSTANTS must be used without single quotes, but when defining a constant. Single quotes are needed.
define('HOSTNAME', 'localhost');
echo HOSTNAME;
・mysqli_connect()
connecting to MySQL database
$conn = mysqli_connect('localhost', 'root', 'password', 'db_name');
PHP functions I have mastered
- ucfirst() // capitilizes only the first letter or the first word of string, the rest will stay the same
- ucwords(); // frist letters of all words in a string will be CAPITALIZED;
- strtolower(); // makes all letters lower case
- strtoupper(); //makes all letters upper case
- strlen() // counts the number of characters in string
- strrev(); //revirses the string
- str_replace(“word”, “replacing word”, $string) // replaces SPACES etc in an a word
- in_array();
- is_null(); // makes VALUE NULL
- explode(); // converts string to an ARRAY
- implode(); // converts an array into string
- unset($variable); //unsets the VARIABLE and if u echo it out you will get error because that variable has been unset
- strcmp(); // string compair function; If 1st string is shorter than 2nd string then value will be NEGATIVE and if 1st is longer than string 2nd string, then value will be POSITIVE number; IF both strings are equal then value will be 0 (ZERO); COMPARISON is done based on ASCII values.
- str_shuffle(); // shuffles string and even numbers
- strpos(); // (case sensitive) find starting position of a string
- stripos(); // (case INSENSITIVE) find starting position of a string
- wordwrap($string, 3, “<br>”, true); // breaks all words and sentences at specified length (3 characters);
- substr($inputText, $start, $length); // cuts out specified part of a string
- is_numeric(“12”); // this doesn’t make NUMERIC values, but it just checks if the string in it is NUMERIC or not; If it is NUMERIC (with or without “”), then it will display 1 as a result; In case it’s a string then it will not ECHO out anything because value will be ZERO and ZERO is usually not echoed out;
- intval(); // truns alfanumeric number like (234bs) into integer. In this case just numeric part will be displayed instead of “234bs” it will echo out “234”.
- preg_match(/pattern/, string, array); // finds a patter(word) in a string; just one (the 1st one in string); /pattern/i then it will be case insensitive.
- if used with ” i ” like this /pattern/i then it becomes case insensitive;
- if used with ” | ” then multiple strings or charackters or numbers can be searched like /a|b|c|9/i
- if you need to find ALL patterns then used this function instead preg_match_all();
- if you need to get a range of numbers of letters then used with ” [ ] ” like this /[0-9] | [a-zA-Z]; or even just from a to c like this /[a-c]/ or /[a-hA-H]/
- ” ^ ” letters or numbers after this mark should NOT BE INCLUDED like this /[^s]/ this will escape all letters ” s “ or exclude numbers like this /[^0-9]/
- session_start(); $_SESSION[‘username’]=’heikijapan’; Sessions are SUPERGLOBALS and can be accessed from other files very easily. But before trying to access SESSION by name you should start it. Code below explains it:
- session_start();
- echo “Welcome, ” . $_SESSION[‘username’];
- array_pop(); // creates a new variable using the last item in the ARRAY
- unset(); this CLEARS $variables value, kind of resets it’s value
- There are many METHODS for sorting ARRAY VALUES
- sort($array); // this will sort everything by ASCENDING order
- rsort($array); // sorts array in DESCENDING order
- ksort($array); // sorts by KEYS in ASCENDING order
- krsort($array); // sorts by KEYS in DESCENDING order
- $mergedArray($array1, $array2); // this will merge multiple arrays into one new array
foreach (); helps to access every value in an array like this.
// code using foreach loop
$array = ['Heiki', 2024, 'Japan'];
foreach ($array as $values) {
echo $values . '<br>';
}
// RESULT from this code (all values have been accessed)
Heiki
2024
Japan
Associative array (how to print ARRAY keys only and ARRAY values only)
$pets = ['name' => 'fury', 'type' => 'cat', 'age' => 5];
print_r(array_keys($pets)[1]);
print(array_values($pets)[1]);
foreach(); can also be used with associated arrya’s $keys and $values as well
foreach ($pets as $keys => $values) {
echo 'KEY: ' . $keys . ' VALUE: ' . $values . '<br>';
}