Category Archives: PHP

Stats from my media server!

By | Blog, C#, PHP | No Comments

So I recently wrote some code to show me stats on the media file types on my media server, and I thought the internet would like to see the pie chart I made! :3


Ill show my C# code and PHP code when I get the chance to sit down and type it out!

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.