Posts in the “php” category

How to display the APC Opcode Cache information page

I just finished moving the alvinalexander.com website to a newer, faster web server, and in the process I had to install the APC Opcode Cache on the new server. After installing APC, I like to be able to test that it's working, so I wanted to be able to display the APC information web page (APC test page).

To display the APC test/information page, just:

The Twitter API, PHP, and OAuth

Just a quick note today on how to access the Twitter API using PHP, specifically using the Abraham Williams PHP TwitterOAuth library. While the library itself seems very good, I couldn't find much in the way of documentation, particularly a simple "getting started" tutorial, so I thought I'd share this code.

In short, I dug through the PHP files in the root directory of the TwitterOAuth library, eventually creating this simple PHP Twitter client example:

Get a web client's IP address with PHP

I just ran into a situation for a Drupal/PHP client where I wanted to log some access information. There was some unusual access activity on the website, and I want to log IP addresses and URLs for a few days.

As part of that, I wanted to get the web client's IP address, and in PHP you get the client IP address like this:

$ip = $_SERVER['REMOTE_ADDR'];

That returns something like this:

192.168.100.10

If you just need to get a web client's IP address from a PHP script, that's all you need.

PHP random number examples (rand function)

PHP random number FAQ: Can you show an example of the PHP random function?

Sure. To create a random number in PHP, just use a line of code like this:

$r = rand(1,10);

That will generate a random number from 1 to 10.

Real-world PHP random number example

Next, here's a snippet of PHP code that shows how to create a random number in PHP in some real-world code:

PHP include syntax examples

PHP FAQ: Can you show an example of how to use the PHP include syntax? Also, can you show an example of how to include HTTP content into a PHP script?

PHP include syntax to include a file

Sure, I just used the PHP include statement in developing a Drupal theme, and a simple form of the PHP include syntax to include a file looks like this:

<? include 'google-analytics.inc' ?>

This includes the contents of the file named "google-analytics.inc" into my PHP page output.

A MySQL PHP update query example

Nothing major here, just an example MySQL PHP UPDATE query (really, a PHP SQL UPDATE query example that happens to use a MySQL database). For all the things I've done lately with PHP, I think this is the first PHP update query I've written, and I wanted to put an example out here so I can find it easily later.

So, with no further ado, here is a simple MySQL PHP update query example:

PHP printing - How to print variables in a PHP string

Nothing major here, just a quick example of how to print variables in PHP, particularly how to print variables in the middle of a PHP string. When you first get started programming in PHP, how to print variables in a PHP string is a common question, so I thought I'd share some examples here.

There are many ways to print variables in a PHP string, but these are some of the most common approaches:

A PHP cheat sheet (reference page)

PHP cheat sheet FAQ: Can you share a PHP cheat sheet?

This page is a PHP cheat sheet, but a different kind of cheat sheet. As I go deeper and deeper down the PHP rabbit hole, I found that instead of an over-simplified PHP cheat sheet, I wanted a "starter" page that will point me in the right direction when I'm looking for a PHP function for a task. For instance, if I can't remember how to add an element to a PHP array, I want to be able to search this page and find that easily.

A simple PHP form example

PHP form FAQ: Can you share an example of a very simple PHP form (a self-processing PHP form)?

Still trying to catch up on a lot of PHP examples I've written but never published, here's the source code for a simple PHP form example. This form posts back to itself, and while I'm generally not a fan of that coding approach, I have used it from time to time for simple tasks.

A simple PHP form example

Here's the source code for this simple PHP form example:

PHP Zend starter applications

I'm going to be starting a new PHP project later this week, and as I don't like to reinvent the wheel, I've been looking around for some decent PHP starter applications, i.e., PHP skeleton applications.

As I've looked around for PHP starter projects, this starter application from Grover Ponders looks the best. It seems well-organized, and you can just download a tar.gz file and get to work.

PHP preg_match regex examples

PHP regex FAQ: Can you share some PHP regex examples using the PHP preg_match function?

For a recent PHP program I was writing, I experimented with the PHP preg_match function and some regular expressions, trying to get the behavior I was looking for. Now that they're working properly, I thought I'd share two simple examples of my "PHP regex" preg_match examples/experiments.

A PHP database metadata example

PHP database FAQ: How do I access the metadata for a database in PHP? That is, for a given database, how do I list the database tables, and fields for those tables?

Solution: I just started this script a little while ago, but even with my limited knowledge of PHP it was very easy to get access to the database metadata, including the database tables and the fields in each table.

PHP array - How to loop through a PHP array

PHP array FAQ: How do I iterate (loop) through a PHP array?

Answer: The easiest way to loop through a PHP array is to use the PHP foreach operator. If you have a simple one-dimensional array, like the $argv array which lets you access command line arguments, you can loop through it like this:

CakePHP naming conventions

CakePHP naming conventions FAQ: Can you share some examples of the CakePHP naming conventions, specifically the CakePHP model, view, and controller naming conventions?

As I get back into the CakePHP development world, I wanted to make a little CakePHP naming conventions reference page, showing examples of the standard CakePHP naming conventions for CakePHP model, view, and controller elements.

A Java CRUD generator example

I'm still spending a couple of hours a day working on my Java CRUD generator. Technically, because this is all driven by templates, this is also a PHP CRUD generator, and a Python CRUD generator, and in general a programming-language independent CRUD generator, but at the moment it seems easiest to refer to it as a "Java CRUD generator", so I'll call it that for the moment.

How my CRUD generator works

In today's update I'd like to show you how this works. In short, you start with a database table like this one:

A PHP array to CSV string example

PHP array to CSV FAQ: How do your convert a PHP array to a CSV string?

Converting a PHP array to a PHP CSV string is so easy, it actually felt like I trick question when I just found the answer in the PHP docs.

In my case, I just had a PHP array named $tags, and I wanted to convert it to a PHP CSV string. To do this, all I had to do was use the PHP implode function, like this:

A PHP "select from where in" query example

If you ever need the source code for a PHP "SELECT FROM foo WHERE id in (1,2,3)" example, I hope the following PHP function will be a decent example to work from. I turned a given list of database table id values into a CSV list, then use that string in a "SELECT FROM ... WHERE ... IN ... (list)" SQL query.

I'm not going to explain it today, but I will share the PHP source code for your reading enjoyment (and problem solving).

PHP cannot redeclare function error message

PHP function FAQ: Help, I'm getting a PHP cannot redeclare function error message, how do I fix it?

Assuming you don't really have two PHP functions defined with the same name, this "cannot redeclare" function error message is usually caused by using the require or include functions to include the same common file (and therefore its functions) more than once.