# Single Quote vs Double Quote:
PHP will not parse anything when a string is in a single quote.
$name = "Nahid"; $string01 = 'my name is $name \n'; echo $string01; echo "\n"; $string02 = "my name is $name \nHello !"; echo $string02;
#Heredoc PHP:
Heredoc PHP syntax is a way to write large bloc of text inside PHP
/** * Heredoc PHP syntax is a way to write large bloc of text inside PHP */ $aboutMe = <<<EOD I am Nahid Mahamud I am a full-stack Web Developer I live in Dhaka, Bangladesh EOD; echo $aboutMe; //Hredoc also parse variables $myName = "Nahid Mahamud"; $myJob = "full-stack Web Developer"; $myPlace = "Dhaka, Bangladesh"; $aboutMe = <<<EOD I am {$myName} I am a {$myJob} I live in {$myPlace} EOD; echo $aboutMe;
#Now Doc:
/** * NOW DOC -> WORKS LIKE SINGLE QUOTE STRING * WILL NOT PARSE VARIABLES AND STRING LITERAL */ $aboutMe = <<<'EOD' I am Nahid I am a UX/UI designer I live in Dhaka EOD; echo $aboutMe;
#ASCI Value:
/** * will print ASCI value from A - Z */ $A = ord('A'); $Z = ord('Z'); for($i=$A; $i<=$Z; $i++){ $letter = chr($i); $asciVal = ord($letter); echo $letter ." -> ". $asciVal; echo PHP_EOL; }
#substr In String:
/** * SUBSTR in String */ $greetings = "Hello World"; //will print -> Hello echo substr($greetings,0,5); //will print -> World echo "\n"; $length = strlen($greetings); echo substr($greetings,($length-5),$length);
#Reverse String In PHP:
/** * REVERSE STRING */ $greetings = "Hello World"; $length = strlen($greetings)-1; for( $i = $length; $i >= 0; $i-- ){ echo $greetings[$i]; } ///simple way $greetings = "Hello World"; echo strrev($greetings);
#String Token In PHP:
/** * STRING TOKENIZATION * JOIN & IMPLODES -> DO THE SAME THING */ $greetings = "Hello World! How are you?"; $parts = explode("!",$greetings); print_r ($parts); $parts = explode(" ",$greetings); print_r ($parts); $original = join(" ", $parts); echo $original; //split by character $parts2 = str_split($greetings); print_r($parts2); //iterator $greetings = "Hello Dhaka, How are you?"; $parts3 = strtok($greetings, ", "); while( $parts3 !== false ){ echo $parts3."\n"; $parts3 = strtok(", "); }
#Search Inside String:
/** * SEARCH SOMETHING INSIDE A STRING * strpos is case sensetive * stripos is -> case insensetive * Strripos will do reverse search */ $quote = "Jack of all trades, master of none"; $quote2 = "Jack of all Trades, master of none"; echo strpos($quote, "trades"); echo stripos($quote2, "trades");
#Search & Replace:
* SEARCH INSIDE STRING & REPLACE */ $quote = "Jack of all trades, master of none"; $replaceString = str_replace('trades','tasks',$quote); $replaceString2 = str_replace(array('trades','master'),array('tasks','expert'),$quote); echo $replaceString; echo PHP_EOL; echo $replaceString2;
#String Trimming:
/** * STRING TRIMMING * trim, ltrim, rtrim * will remove nonredable charchaters * ltrim -> will trim left sided characters * ttrim -> will trim right sided characters */ $string = " hello \n"; //this will only trim spaces $string2 = trim($string,' '); //this will trim whitespaces + \n \t.. $string = trim($string); echo $string2; echo $string;
#Word Wrap In PHP:
/** * WORDWRAP IN STRING */ $string = "Life is like a hurricane here in Duckburg. Race cars, lasers, aeroplanes - it's a duck blur."; echo $string; echo PHP_EOL; echo PHP_EOL; echo wordwrap($string, 34); echo PHP_EOL; echo PHP_EOL; echo wordwrap($string, 14, "\n", true);
#NL2BR:
/** /** * NL-2-BR * convert \n into a newline/break for HTML pages */ $string = "Life is like a hurricane \nhere in Duckburg. \nRace cars, lasers, aeroplanes - it's a duck blur."; echo nl2br($string);
#sscanf:
/** * sscanf */ $name = "Nahid Mahamud 0212190022"; $parts = sscanf($name, "%s %s %10s"); print_r($parts); $partsWithVar = sscanf($name, "%s %s %10s", $fname, $lname, $phone); echo $fname; echo PHP_EOL; echo $lname; echo PHP_EOL; echo $phone;