$words = explode("\\n",join('',@file("words.txt")));
What does the above code do ?
gets you the words from words.txt,
if you have one, and strips off the newline character, before assigning each word to array element. otherwise you may need array_walk or foreach to do this..
Code Snippets
php tips
Ever wondered how to capture the user input when writing php command line scripts ?
<?php
function getInput($msg){
fwrite(STDOUT, "$msg: ");
$varin = trim(fgets(STDIN));
return $varin;
}
?>
The function above is being used by me in certain command line scripts, where I need user responses.
Code Snippets, PHP
php command line, php tips, php5-cli
The code: Source File in text
I usually use this to check the generation time for any page. There are critical applications which we are developing and will need less than a milli second generation time.
Code Snippets, PHP, Uncategorized
php snippets, php tips
Our team was having a debate on why we should put a ‘ (single quote) when referencing arrays in php. Well what is the problem if we use $_SESSION[key], instead of $_SESSION['key'].
Well when you think that when encountering non functions or keywords, php tries to evaluate it as a declared constant. So referencing $_SESSION['key'] would be better against $_SESSION[key], unless we have defined the constant key and want to use the value of key.
PHP, Tips
php tips, speedup php pages
I recommend using ‘ (single quotes) when programming with PHP; Always use ‘ (single quotes) unless you need the features of ” (double quotes). You might think it’s much easier to write code as:
echo "Today is the $day of $month";
However, using single quotes forces variables to be outside the quotes; instead when you use the ” (double quotes), forces php to evaluate the string, where as with ‘ (single quotes), the string content is taken as such.
PHP, Tips
php tips, speedup php pages
The single most important thing I tell people who use PHP is to turn error reporting to its maximum level. Why would I want to do this? Generally the error reporting is set at a level that will hide many little things like:
- declaring a variable ahead of time,
- referencing a variable that is not available in that segment of code, or
- using a define that is not set
These factors might not seem like that big a deal — until you develop structured or object oriented programs with functions and classes. Too often, writing code with the error reporting turned up high would cost you hours as you scoured long functions that didn’t work because a variable was misspelled or not accessible.
PHP won’t tell you anything in that case it’ll just create the new variable for you and initialize it to zero. The remedy is to put the following line at the top of every PHP document as you develop:
error_reporting(E_ALL);
It simply forces the error reporting to be at its highest level. Try putting this line in other PHP programs, and more often than not you’ll receive a barrage of warning messages that identify all the potentially wrong elements of the code.
Eventually this when enabled, will force you to write cleaner, structured and fast code.
PHP, Tips
error reporting, fast code, php tips
Recent Comments