You are browsing the archive for Basics.

by Steven

Generating Random Numbers in PHP

June 10, 2011 in Basics, PHP

To create random numbers in PHP, you can use the rand() function call, which takes either zero or, optionally, two parameters determining the minimum and maximum random numbers (inclusive) that you'd like. If you don't specify a maximum then it will default to a value, RAND_MAX, set in the PHP configuration. The function returns an integer, generated "randomly." (Computers can't generate real random numbers, but this tries and is suitable for most purposes.)

You can get the value of RAND_MAX with the function getrandmax(). Then if you desire a random number larger than that, you have to specify the min and max bounds as parameters, eg rand(0, 100000) will return a random number between 0 and 100000 inclusive - so both 0 and 100000 can be returned.

For example:

<!--?php // random number between 0 and RAND_MAX:
echo rand(); // output: 28688 (for example)
// random number between 37 and 50:
echo rand(37, 50); // output: 44
// find out the value of RAND_MAX:
echo getrandmax(); // output: 32767
// get a larger random number by specifying min and max:
echo rand(0, 1000000); // output: 351990
?>

The output shown in the comments above is as an example. Since it is generating random numbers, the numbers generated are very likely to be be different every time.

How To: Find Items in PHP Arrays

April 4, 2011 in Basics, PHP

The problem: we have an array of items in PHP and we want to find out if a specific item is in the array. In code we can define the array as:

IE we have an array called $fruitBasket which contains 5 strings, each of which is the name of a fruit. We want to find out if there is an Apple in the $fruitBasket - is there an element "Apple" in the array?

We do this with the following code:

<?
// create an array of strings called $fruitBasket:
$fruitBasket = array( "Apple", "Orange", "Mango", "Lemon", "Pear" );
// use the in_array() function to check if "Apple" is in the array:
if( in_array("Apple", $fruitBasket) )
{
  echo "Apple is in the array";
}
else
{
  echo "Apple is not in the array";
}
?>

This code uses the in_array() method to check if the element "Apple" exists in the array $fruitBasket:
in_array("Apple", $fruitBasket)

in_array() takes two parameters here: firstly the object we're looking for, in this case "Apple", and secondly the array which we're looking in, $fruitBasket. It then returns a boolean value: true if "Apple" is in the array, or false if it isn't.

References: php.net manual: in_array() function

What is the difference between require() and include() in PHP?

April 4, 2011 in Basics, PHP

The key difference between require() and include() is that if you require() a file that can't be loaded (eg if it isn't there) then it generates a fatal error which will halt the execution of the page completely, and no more output will be generated. On the other hand, if you include() a file that can't be loaded, then this will merely generate a warning and continue building the page.

What one you should use depends on the situation; require() is best suited for loading files that are essential to the rest of the page - for example if you have a database driven website then using require() to include a file containing the database login and password is clearly preferred over using include(). If you used include() in this situation, then you may end up generating more warnings and errors than you had intended.

include() should be used when it isn't essential for that file to be loaded to execute the page. This is best used in situations where the file isn't essential to the processing of the page (for example a footer file), so if the file isn't present then the user can still view the site. You should, of course, make sure that all files you include() and require() are going to be available.

In PHP versions prior to 4.0.2. there was slightly different behaviour of require(). If you used a require() statement in an if block then the require() statement will always make sure that the file you're require()ing is readable, regardless of whether the condition was true for that if block to be processed. This is best illustrated with the following code example:

<?
$a = 1;
  if($a == 2) {
    require("header.php");
  }
?>

In this example, PHP versions before 4.0.2. will always make sure that header.php is available, but it will only actually execute the contents of it if $a is equal to 2.

From php.net:

Note: Prior to PHP 4.0.2, the following applies: require() will always attempt to read the target file, even if the line it's on never executes. The conditional statement won't affect require(). However, if the line on which the require() occurs is not executed, neither will any of the code in the target file be executed. Similarly, looping structures do not affect the behaviour of require(). Although the code contained in the target file is still subject to the loop, the require() itself happens only once.

How to use PHP in pages with a .html extension (or any other)

April 4, 2011 in Basics, PHP

To be able to use php code on a page with an extension other than .php, you need a server which supports .htaccess files.

To add an extension to be parsed for php, create or edit a file called .htaccess (with the dot first) containing the following line:
AddType application/x-httpd-php .html .moo .htm
This tells the server to check for php code and execute it in files with extensions .html, .moo or .htm. So upload that .htaccess file in the same folder as your .html files with PHP code inside them (using PHP tags: <?php ... ?>) then when you view them in the browser, the PHP should be dealt with as you'd expect from a PHP page.