Home >

Blog Archives

Understanding PHP Constants

A constant within PHP is essentially an identifier for a simple value, and unlike a variable, the value of a constant cannoyt be changed after it is declared. By default a constant is case-sensitive; and most people tend to give constant identifiers uppercase names. To get started you will want to define a constant using the define() function, like so: define("SITENAME", ...

Read More »

The Basics of PHP Classes and Objects

As you learned in previous tutorials functions provide a way for you to divide your program’s tasks into separate, smaller tasks. Classes and objects are somewhat similar in helping you to organize your program. They are used to create collections of related variables and functions, which can be used to more accurately represent real-life situations. So you might have a ...

Read More »

FOR and FOREACH Loops

Much like WHILE loops FOR and FOREACH loops are meant to enable the repeated execution of a section of code. However, unlike while statements, for and foreach have features built into them to update the sentinel value for every repetition. So lets go back for a moment to our original example in the WHILE loops tutorial – Bart Simpson having to write ...

Read More »

String Manipulation – Using SUBSTR & STRLEN PHP Functions

Now that we’ve covered the basics of what functions are within PHP – PHP has a wide array of functions already available for you to use to work with various strings. So let’s say that we have a variable holding a user’s telephone number and we’re asking our user to input the last 4 digits of their phone number to verify ...

Read More »

The PHP Switch Conditional Statement

The switch statement allows you to simplify our previously written if…elseif…else statement by not constantly writing out the condition to check for. So, let’s take our code: <?php $name = "Eric"; if ($name == "Mike") { echo "Name Is Mike B."; } elseif ($name == "Eric") { echo "Name is Eric A."; } elseif ($name == "JD") { echo "Name ...

Read More »

Understanding IF / ELSE / ELSEIF Statements

The majority of the time your PHP application or script will vary depending on different conditions. These conditions can be set via the script itself, or from user interaction – regardless your PHP script needs a way to handle these different situations. This is where conditional statements in PHP are introduced. PHP has several different conditional statements: if statement – ...

Read More »

Connecting To A Database Using PHP Data Objects

With the release of PHP 5.5.0 the mysql_* functions have become deprecated and as such connecting to a MySQL database with PHP 5.5 and up will require the use of either PHP Data Objects (PDO) or an individual MySQL driver (MySQLi). Throughout this tutorial we will focus on utilizing PDO to connect, access, and disconnect from a MySQL database. If you’re ...

Read More »

Including Another File Within Your Script

Almost all online scripts and applications will feature a core library of variables and functions that get utilized throughout the script. For example, let’s say you’re writing a database intensive application. You could declare your MySQL details within each page of your script – however if you ever changed your password that would mean that you would need to update ...

Read More »

Making Life Easier with PHP While Loops

The WHILE loop is one of the most useful commands in PHP. It is also quite easy to set up and use – granted it might look a bit complicated. First off however let’s figure out just what the hell a WHILE loop is – and what we need it for. A WHILE loop will, as the name suggests, execute ...

Read More »

Sending Mail with PHP

Mail is extremely easy to send from PHP, unlike using scripting languages which require special setup (like CGI). There is actually just one command, mail() for sending mail. In this tutorial we will discuss how to use PHP’s mail() function. It is used as follows: mail($to,$subject,$body,$headers); In this example I have used variables as they have descriptive names but you could ...

Read More »

PHP/MySQL File Upload – Error Checking

A while back I had written a PHP/MySQL tutorial discussing how to upload an image to your server, and store the information about that uploaded image into your MySQL database. That tutorial covered really only the basics, and I knew that eventually someone would post a comment asking about some more advanced features – such as limiting the file size of ...

Read More »

How PHP Functions Work

A PHP function is merely a block of code that can be defined once and then reused throughout various other parts of your script/program. Typically, a function takes an argument, or a series of arguments, and performs a predefined set of operations on them – returning a value as a result. With PHP functions, code that would otherwise be repeated ...

Read More »

The PHP Ternary Operator

PHP has plenty of different operators, the majority of them being either unary or binary operators. A unary operator, such as !, performs its operations on just one single value. A binary operator, such as =, is used to perform an operation on two operads. So following the naming logic you’ve probably already figured out that a PHP ternary operator ...

Read More »

File Upload via PHP/MySQL

As per the request of Spaculus I have decided to put together a tutorial discussing how to upload an image to your server, and store the information about that uploaded image into your MySQL database. This is a function that can often come in handy when developing a user membership system with avatar support, or a venue/event listing that displays an ...

Read More »

Escaping Characters and Variable Expansion

Escaping characters is basic, but essential to an error-free PHP script. Consider the following sentence for example: Mike’s uncle said, “Isn’t that cool?” Now if we attempt to use PHP to echo this sentence to the browser, those quotes and apostrophes will cause some problems. The quotes and apostrophes inside the string are confused for the symbols used to signify ...

Read More »

Connecting to a MySQL Database

None of our previous PHP tutorials have dealt with a database yet, and that is for the sole reason that not everyone has access to a database, however for those who do this tutorial will teach you the basics on how you can connect to your database. We wont be covering SQL queries and the conversion of SQL queries into ...

Read More »

PHP Variables – Understanding The Basics

If you have ever looked through any programming language – or still remember those algebra classes when you learned what 3+x=5 means then you already know what a variable does. A variable stands for something else, or in PHP it stores something inside of it so that you can recall it later on in your scripting. So let’s take a ...

Read More »

Getting Things Started With An Echo();

Chances are you’re familiar with the Hello World exercise by now. It’s the starting point for learning almost any programming language. PHP is no different. So let’s dive right into it. How do we display things using PHP? This is post #1 of 20 in the series “PHP Tutorials: The Basics” The Basics PHP Tutorial series is aimed at giving ...

Read More »

Like every other website, this site uses cookies to analyze our traffic. Cookies may also be utilized by our advertisers and partners. By using this website you agree to the use of said cookies. More Information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings, continuing to navigate past this message, or you click "Accept" below then you are consenting to the use of cookies on the Rapid Purple website.

Close