Archive for November, 2007
Number Formatting
Oftentimes, you have a numeric value, but it is not in the format you want to display. The average cost of houses in Timbuktu, for example, might be 123456789 / 1111 or 111122.22232223 — so how do you change that to $111,122.22 ?
Very simply, just use:
$price=number_format($p,2,”.”,”,”);
Assuming $p is 111122.22232223 this function outputs a formatted string [...]
Filed under: Functions | Leave a Comment
Yesterday we looked at a couple of the more useful string functions, substr() and strpos(). We ended that post with a simple test, to see if you understood the functions: How do you get the end of the string?
Well it’s simple, as mentioned earlier in that post, the third variable is optional in the substr() [...]
Filed under: Functions | Leave a Comment
Let’s look at the substring and stringposition functions today. The syntax is easy for these useful little functions:
$result=substr($work,$start,$length);
$result=strpos($work,$searchfor,$start);
Suppose our original text is ‘What the heck?’
$work=’What the heck?’;
We want to find out what the first word is. First, we need the second function — stringposition — to find out where the first space is:
$length=strpos($work,” “);
PHP [...]
Filed under: Functions | Leave a Comment
Query Function
So last time we learned how to make a function, but it didn’t really do much. Perhaps it got you away from thinking a function had to return some useful value — they may, but it might just as well perform a useful task, as in that example.
Today, we will look at another function that [...]
Filed under: Functions | Leave a Comment
Tags: mysql queries
PHP Functions
PHP has more than 700 built-in functions. More importantly, PHP allows you to make your own functions.
Functions are pieces of code that do some specific task. Here is an example of a very simple function:
function p($work)
{
echo(“$work\n”);
return 1;
}
Spaces and line breaks are ignored by PHP, so this could just as well be written as one line [...]
Filed under: Functions | Leave a Comment
Tags: built-in functions, user functions
Search
-
You are currently browsing the PHP Basics weblog archives for the month November, 2007.