PHP: Decision Making

This part is of the PHP Post Series dedicated to spread knowledge about PHP

There are two types of decision making in PHP.

  1. The if statements
  2. The switch statements

If Statement

The if statement consists of boolean expression(s) to be evaluated to make a decision.

Here’s some pseudocode

if (boolean expression) {
  // statements here will be executed if boolean expression evaluates to true.
}

If the boolean expression is true, then the statements in between the if statement is executed. If the boolean expression doesn’t evaluate to true, the statement in between the if statement doesn’t get executed and the next line after the if statement starts execution.

if (1 == 1) {
    // This gets executed
    echo "I did not know that!";
}

This will produce:

I did not know that!

If..Else Statement

In an if..else statement, whenever the boolean expression evaluates to false, the statement in the else block gets executed.

Here’s some pseudocode

if (boolean expression) {
  // statements here will be executed if boolean expression evaluates to true.
} else {
  // statements here will be executed if boolean expression evaluates to false.
}
if (1 == 2) {
    // This gets executed
    echo "I did not know that!";
} else {
    echo "I knew that!";
}

This will produce:

I knew that!

 PHP: Including and Requiring Files

This part is of the PHP Post Series dedicated to spread knowledge about PHP

Code organization is one of the reasons for including and requiring files in PHP applications. This is mostly used for configuration files, settings, etc.

Include

include() is a function that allows you to include files in PHP files.

Example Code
include('simplepie.inc');

Include Once

include_once makes sure that a file is included only once in the file. If the file was already included, the file won’t be included again.

include_once('simplepie.inc');
Include / Include Once Trick

include and include_once can also be written as follows if we want to check if a file has been included.

if (@include('simplie.inc')) {
    echo 'File has been included';
} else {
    echo 'File has not been included';
}

Require

Another function to be used when including files is the require function. The require function doesn’t just include a file but requires it. This means that if a file fails to be included, probably due to the file not existing, a fatal error is thrown.

require('simplepie.inc');

Require Once

require_once just like include_once makes sure that a file is included only once.

require_once('simplepie.inc');

 Rename a Table in Rails

Sometimes you aimlessly create a table and you’d want to change it. Here’s how to rename a table in Rails 3 and higher.

Rails 3

class RenameOldTable < ActiveRecord::Migration
  def self.up
    rename_table :old_table, :new_table
  end
  def self.down
    rename_table :new_table, :old_table
  end
end

Rails 3.1+

Rails 3.1 and above offers a shorter alternative to renaming tables

class RenameOldTable < ActiveRecord::Migration
  def change
    rename_table :old_table_name, :new_table_name
  end
end
 

 PHP: Delete files with unlink()

This part is of the PHP Post Series dedicated to spread knowledge about PHP

Deleting, creating and modifying files critical filesystem functionalities you can expect from programming languages.

PHP doesn’t want to get left down. Here are a few functionalities and tricks for deleting files.

Delete a single file

unlink('index.html');

Delete all files in a directory

// get all file names
$files = glob('path/to/folder/*');
// loop through files
foreach($files as $file){
  if(is_file($file)) {
    // delete file
    unlink($file);
  }
}
// A better alternative is to use the array_map function in conjunction with the glob function
array_map('unlink', glob("path/to/folder/*"));

Deleting certain file types

$jpeg_files = glob('path/to/folder/*.jpg');
// loop through files
foreach($jpeg_files as $file){
  if(is_file($file)) {
    // delete file
    unlink($file);
  }
}
// Alternative
array_map('unlink', glob("path/to/folder/*.jpg"));