PHP Programming Basics Introduction

When learning to program, it is really important to understand the basics! So let's get into it.

<?php
/**
 * The Main Controller
 *
 */
session_start();
define('BASE_PATH', $_SERVER['DOCUMENT_ROOT'].'/');
define('BASE_URL',  'http://'.$_SERVER['HTTP_HOST'].'/');
include "./includes/functions.php";

$uri_request = $_SERVER['REQUEST_URI'];
// It is important to remove any leading or trailing slashes
//    so the explode produces actual values
$uri_request = trim($uri_request, '/');
// Extract the bits from this string
$segments = explode('/', $uri_request);
$page_name     = isset( $segments[0] ) ? $segments[0] : null;
$page_category = isset( $segments[1] ) ? $segments[1] : '';
$page_category_name = '';
//If we have a category, use it else set it to blank so it is ignored
if($page_category !== '') {
	//Do we have a query after the cat section
	$chop_cat_pos = strpos($page_category,'?');
	if($chop_cat_pos !== FALSE){
		// Yes we do so Chop it!
		$page_category = substr($page_category,0,$chop_cat_pos);
	}
	$page_category_name = '/'.$page_category; // This is the URL Version
	$page_category = $page_category . '-'; // This is the Filename Version - No Leading / and Add the hyphen
}

if($page_name !== null) {
	// If we just get articles, then we want to display them all
	if($page_name == 'article') {
		//load the article list in the layout
		$content         = display_article_list();
		$page['body_id'] = 'Articles';
		include( './layouts/master_layout.php' );
		exit();
	} else {
		$path_article = './articles/' . $page_category . $page_name . '.php';
		$path_views   = './views/' . $page_name . '.php';
	}

	if(file_exists($path_article)) {
		ob_start();
		/** @noinspection PhpIncludeInspection */
		include( './articles/' . $page_category . $page_name . '.php' );
		include( './layouts/single_post.php' );
		$content .= ob_get_contents();
		ob_end_clean();

		// Add to session link list
		set_link_history($page_category.$page_name);

	} elseif(file_exists($path_views)) {
		ob_start();
		// just display the home page
		include( './views/' . $page_name . '.php' );
		$content .= ob_get_contents();
		ob_end_clean();
	} else {
		$article_names = get_files_from_directory('./articles/');
		$content       = '';
		// Build up each post
		foreach ( $article_names as $article_name ) {
			ob_start();
			// Build the Individual Post
			/** @noinspection PhpIncludeInspection */
			// Get the Articles information
			include( './articles/' . $article_name );
			/** @var array $page */
			// We need to grab the 1st paragraph
			// If we find an opening <pre> tag indicating we have syntax highlighted code,
			//   we dont want to show that. So we use that as the offset.
			// else we just go looking at our designated offset
			$pos = strpos($page['content'], '<pre>', 0);
			if($pos === false) {

				$content_length = strlen($page['content']);
				$offset = 400;
				if($content_length < $offset){
					$offset = $content_length - (int)($content_length *.75);
				}
				//@todo need to determine the comment length and decide on the offset
				//   based upon this we can go hunting BUT what if we don't find a match?
				//   Terminating characters are . , ! , ?
				$pos             = strpos($page['content'], '.', $offset);
				$page['content'] = substr($page['content'], 0, $pos + 1);
			} else {
				$page['content'] = substr($page['content'], 0, $pos);
			}
			// Add a Read More Link
			$page['content'] .= '<br>';
			$article_name_no_extension = str_replace('.php', '', $article_name);
			$page['content'] .= '<a href="' . BASE_URL . $article_name_no_extension . '">Read More!</a>';
			$page['content'] .= '<hr>';

			$page['comments'] = '';

			// Build the Post

			$meta['keywords']    = 'tutorials,php,jquery,ajax';
			$meta['description'] = 'Learning how to program by example.Tutorials on PHP, MySQL,AJAX etc.';
			include( './layouts/single_post.php' );
			$content .= ob_get_contents();
			ob_end_clean();
		}
		$page['body_id'] = 'Home';
	}
}
include( './layouts/master_layout.php' );