Archive

Archive for November, 2008

Basic PHP Syntax

November 25th, 2008

When a PHP Programmer starts to write a PHP snippet, the basic php syntax always starts with <?php and ends with ?>. You can place this snippet anywhere in the *.php document.

Some servers do support shorthand, in which case you can start with<? and end with ?> .

But for maximum portablitlity, I do recommend that you stick with the standard <?php rather than shorthand <? .

Heres an example:

<?php

echo “Hello, World”;

?>

4. Basic PHP Syntax

Basic PHP Training

November 20th, 2008

Today’s market is getting inundated new PHP websites each day. This means that there is a flux of new PHP Programmers joining the developers in creating and editing these new sites. What we will do is help these new developers gain an understanding of what the basics of PHP Programming are.

Here are a few of the materials covered in the Lessons:

Training

PHP Basics

November 20th, 2008

For the PHP Basics we are going to assume that your server has basic PHP support and that .php extensions are handled by PHP. If this is the case you’re ready to start as a PHP Programmer. One of the things I love about PHP is that I don’t have to compile anything. I don’t need any extra tools if I don’t want. Just a text editor and ftp program.

I like to think of PHP files as simple HTML documents that allow us to create tags to make the content come to life, dynamically. All you have to do is write the code and save the file to your server. That simple.

As you become more proficient and leave the PHP Basics, you will learn other tools and techniques to write your own code. For now we will just show you what it is to be new to the language.

Our next section covers How PHP Works. Thank you for taking time to read our PHP Basics section.

1. PHP Basics

How PHP Works

November 20th, 2008

When you browse to a website such as http://ctcoder.com/index.php, your ISP directs you to the server that holds the website information/files for CTCoder. The server will then read that index.php file and process the information contained within. PHP then tells the Client browser in the form of html. What this means is that PHP will create an HTML page dynamically based on what instructions the PHP Programmer wrote in the file.

This data is in the form of HTML that the browser can display as it would a standard HTML page. In short, PHP creates an HTML page on the fly based on parameters of my choosing; the server contains no static HTML pages.

In our next section we will cover what the php.ini file is used for: 3. The php.ini File

2. How PHP Works

Using a php.ini File

November 20th, 2008

This will show you how to customize the php.ini configuration file. This will control some settings of the PHP interpreter. This file enables you to such as global variables, register_globals.

How a php.ini File is Read

When PHP starts, it follows and behaves according to the specified settings of the available php.ini file. The server will look for this file in these locations in this order:

1. Within the directory that the page was loaded
2. The root directory (public_html or www folder)
3. The server’s default php.ini

Your server’s php.ini file will be used if you dont have a custom one created. You can find the default one in /usr/local/lib/php/php.ini. Check out these settings in the file as it may help you find the solution to problems you may be having on your server.

register_globals = On

The register_globals setting controls how users access your forms, server, and other variables. This variable is set to ‘off’. This means that you will have to use special arrays in order to access these variables.
NOTE: The reason for this is because when register_globals is set to On, your PHP scripts will become more vulnerable to attacks.

To retrieve the value of [input name="formVar"] from a form submitted with the POST method:

When register_globals = On

$theVariable = $formVar;

When register_globals = Off

$theVariable = $_POST['formVar'];

3. The php.ini File

Cheap Web Hosting

November 18th, 2008

From this point forward real estate on the internet in the form of domains will become the most sought after property in the business marketplace. For any cheap web hosting service can widen your profit margin.

Location is still the most important part of any business venture but consumers have fled the malls and shopping centers for the infinite space of online e-commerce and having a viable and useful virtual storefront is essential to surviving in the tech-savvy world of tomorrow. Consider a web hosting sever as your commercial landlord, the entity that allows you the space to house your wares and goods and the dynamics to conduct a thriving business.

Cheap web hosting is fast becoming the foundation for any successful online venture. The lifeblood of any internet business is the ability to maintain occupancy at a certain domain and build up that property with products and information that will keep customers coming back. Paying out enormous monthly fees and high rents for dedicated support of your domain on a server can seriously infringe on your bottom line profit. Being able to affordably dock your site on a secure server through cheap web hosting will allow you to conduct your business operation efficiently and cost-effectively.

Cheap web hosting doesn’t mean less service. Even with a basic set up service you will still benefit from an active domain, technical support on your site and a certain amount of bandwidth that will allow you to complete transactions, fulfill orders and run your business without glitches and hassle. Having a cheap web hosting service allows you to run your business online without worrying about technical problems.

When selecting a web hosting service for your business consider how large your site will need to be, how much traffic is expected, how many email accounts you’ll need to effectively run your operation and how integral your web site will be to the overall success of your business. Cheap web hosting is available for retail, manufacturing, finance and many other businesses but how involved the web will be in any endeavor will vary. Finding a cheap web hosting service that takes care of your specific needs is essential to matching your business plan with a productive online division.

Cheap web hosting is available through many web hosting companies and each has tailored programs to run compatibly with your specific e-commerce operation. It also helps to think of web hosting servers in terms of affordable rather than cheap since many low cost server providers offer excellent support and the options to develop your site as you grow to adapt to a changing marketplace and new products and services.

Hosting

Fetch Data From MYSQL Using PHP

November 16th, 2008

$query = “SELECT * FROM table”;
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_array($result)){
echo $row['column1']. ” - “. $row['column2']. “
“;
}

This script will come in handy for the new PHP Programmer. You can fetch information from the database and display it directly to your browser. These scripts are made for simplicity. I may someday write a tutorial but as you know there are plenty of tutorials for a PHP Programmer.

Scripts

Connect to MYSQL database using PHP

November 16th, 2008

mysql_connect(”localhost”, “USER”, “PASS”) or die(mysql_error());
echo “Connected to MySQL”;
mysql_select_db(”DATABASE”) or die(mysql_error());
echo “Connected to Database”;

This is a simple mysql db connection script for the PHP Programmer. All you have to do is replace the USER, PASS and DATABASE with your settings. You may also need to change the localhost with your own setting also. These scripts are intended for educational purposes. Feel free to use them as you need.

OOP Programming is becoming more and more popular among web owners and programmers. Here is an example for getting data from the db using OOP PHP Programming.

Create a table named our_table and create a field for ID. Now enter a coupe id’s in this new table. When you have your db set create the following files.

Name a file include.php and add this:

include ‘function.php’;
require_once(’DB.php’);

$host = ‘localhost’;
$user = ‘your user’;
$pass = ‘your pass’;
$database = ‘your db’;

$dsn = “mysql://$user:$pass@$host/$database”;
$db = DB::connect($dsn, false);

function GetMainContent()
{
global $db;

$result = $db->query(’SELECT * FROM our_table ORDER BY id ASC’);

$selfArray = array();
while ($row = $result->fetchRow(DB_FETCHMODE_ASSOC))
{
$myContent = new ContentBag($row["id"]);
array_push($selfArray, $myContent);
}
return $selfArray;
$db->disconnect();

}

Now create a file named function.php and add this:

class ContentBag
{

public $id;

function __construct($Id=null)
{
if (!empty($Id))
{
$this->id = $Id;
}

}

}

Now in your view file named index.php add this:

include (”includes.php”);
$get_id = GetMainContent();
echo “\n”;
foreach ($get_id as $text)
{
echo “ID: ” . $text->lid . ”
\n”;
}

Scripts

Send Email with PHP

November 14th, 2008

$EMAIL_HEADER = “From: Sender Name <sender@example.com>\r\n”;
$EMAIL_HEADER .= “MIME-Version: 1.0\r\n”;
$EMAIL_HEADER .= “X-Priority: 1\r\n”;
$EMAIL_HEADER .= “X-MSmail-Priority: High\r\n”;

$MESSAGE=”
Hello there,

This is a test message.

From Admin.”;

mail(”recipient@domain.com”, “Example Subject”, $MESSAGE, $EMAIL_HEADER);

Scripts

Link Without Underline Tutorial

November 14th, 2008

At times we will need to create a link without the underline so that it fits better with our text. Its fairly simple here goes:

PHP Programmer
vs.
PHP Developer

Simply add this style to the css stylesheet for anchor tags to be displayed without the underline or to the link code:

<a href=”#” style=”text-decoration:none;”>Link</a>

These decoration values dont work with all browsers but are worth knowing:

none, underline, overline, line-through or blink

Some of you may be looking for a simple way to write a PHP link, well here goes:

echo ‘<a href=”‘. $linkVar .’” style=”text-decoration:none;”>’. $nameVar .’</a>’;

or

echo ‘<a href=”#” style=”text-decoration:none;”>My Link</a>’;

Basics