PHP Tutorial

PHP: Redirecting

By | PHP | No Comments

Sometimes, you need to setup a structure that checks if a user is logged in. If a user is not logged in, then you should redirect to a login page instead of displaying the rest of the page. There is a very simple way to redirect the page, with the header() function. There are other commands you can send via headers, but for this tutorial we will only show the Location header.

header("Location: index.php");

In place of the index.php, you could put absolute paths, relative paths, or completely different domains as the url for redirection. Very easy to do. The only thing you should be aware of by using the header() function is that the page can not start outputting anything otherwise the header() function will throw an error. Say if you started the code with an echo function that displays text, the headers of the webpage would already be sent has text, and you can not edit the headers after they have already been sent.

PHP foreach tutorial

PHP: foreach loop

By | PHP | No Comments

The foreach loop is a very useful tool that is used with arrays. Instead of making code to count the elements of an array and then doing a for loop on that array using the counts, we can just use the foreach loop. With the foreach loop, we would pass an array as such: “$array as $element”. Code:

$arrayint = array(1,2,3,4,5);
foreach($arrayint as $int){
    echo $int, ', ';
}

Very simple to do. Now lets say you got a bunch of data from a database in which you made the database ids the ids for each element in the array. You can still use the keys from the array in the foreach, as such:

$arrayint = array(1 => "first",
        2 => "second",
        3 => "third",
        4 => "fourth",
        50 => "some random string value");
foreach($arrayint as $key=>$str){
    echo $key, ' => ', $str, ', ';
}

This code will output the key of the element we are on, then output the corresponding string of the element.

Now for the last bit of code that handles multidimensional arrays:

$arrayint = array( array( 1 => "first",
                          2 => "second",
                          3 => "third",
                          4 => "fourth",
                          50 => "some random string value"),
           array("stringkey" => "some value",
                 2 => "2nd element"));
foreach($arrays as $array){
    foreach($array as $key=>$element){
        echo $key, ' => ', $element, ', ';
    }
}

Now this will output everything in a two dimensional array. If there is a third level of arrays, then this will not output it. You would need to add in another foreach loop.

Other notes:

  • All PHP code needs “<?php” before and “?>” after the code to run.
  • The “$arrayint” is a variable. Use the dollar sign in front of a meaningful variable name to create variables in PHP.
  • array() is the PHP function for creating arrays. Use “=>” to create keys like “key => value”.
  • The “echo” function prints whatever values you pass to it in the HTML of the webpage.
  • foreach() is a PHP function that goes through each array element.
Tutorial square roots PHP

PHP: Square roots

By | PHP | No Comments

Doing square roots in PHP is really simple, all you need to do is to call the function sqrt().  Lets see some code.

//Set number you would like to take square root
$int = 9;
//Take square root
$sqrt = sqrt($int);
//Display result
echo $sqrt;

As you can see, simple to do. Now lets do it in a more mathematical way using the PHP function pow(), which is the power function. pow() has two inputs, pow($base, $n). $base is the base number you will raise to the $n power. Now usually, we would use this function to calculate powers like 22 (which is 4). However, if you know your math, we can raise numbers to the power of fractions, which is the same as taking the root of numbers. Not just the square root either, we can use this to find the cube root, fourth root, any root you would like to find. Now lets see some code for finding the cube root of 729.

//Set base number and the exponent
$base = 729;
$exp = 1/3;
//Take root
$sqrt = pow($base, $exp);
//Display result
echo $sqrt;

The $base is the number we will be taking the power of $exp of. This way we can use the pow() function to not only find powers of numbers but the roots as well.

Other notes:

  • All PHP code needs “<?php” before and “?>” after the code to run.
  • The double slash “//” is a comment that PHP will ignore. For bigger comments, use “/* really big comment */”.
  • The “$int” is a variable. Use the dollar sign in front of a meaningful variable name to create variables in PHP.
  • sqrt() is the function that calculates the square root of a float number.
  • pow($float, $exp) is the function calculates a number $float to the power of $exp.
  • The “echo” function prints whatever values you pass to it in the HTML of the webpage.