Site Meter Web Dev Notes » PHP

PHP

PHP Variable Naming Rules

Thursday, May 17th, 2007

While we have much freedom in choosing the names of our variables when writing a program, there are still always a set of rules we must abide by. The following are the PHP variable naming rules which I have taken from w3schools.

  • A variable name must start with a letter or an underscore “_”
  • A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ )
  • A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

Using Concatenation with PHP Variables

Wednesday, May 16th, 2007

Yesterday I introduced PHP variables and demonstrated how they can be set and used by means of a simple example. Today I want to expand on this and show how you can use concatenation to use multiple variables in the same line of code to create more complex and dynamic strings.

In PHP, the concatenation symbol is the period symbol as shown at the end of this sentence -> .

<?php
   $stringVariable = "My lucky number is";
   $integerVariable = 11;
   print $stringVariable .  " " . $integerVariable;
?>

A string and an integer variable are set as demonstrated in the “How to use Variables in PHP” article. The difference this time comes in the print function which uses concatenation to combine the variables into a single sentence.

In the above example, it is not enough to simply concatenate the two variables or else the final result printed would be, “My lucky number is11″. To avoid this problem we could add a space to the end of our $stringVariable but this could cause a new bug if for example we wanted to use the variable at the end of a sentence with a period following directly after. The best way to solve the problem is to therefore use concatenation to add an empty space between both variables when they are printed. The way to do this is shown in the above example by using the set of quotes with a space between them.

How to use Variables in PHP

Tuesday, May 15th, 2007

Variables are very important once you start programming in PHP. Variables are used to represent a value and can store numbers, strings or arrays. The main reason variables are so useful is because they can then be used over and over again as many times as necessary.

Here is a simple example of using a variable:

<?php
   $theVariable = "This is a String";
   print $theVariable;
?>

The above code will set the value of $theVariable and then use the print function to print “This is a String” to the screen.

<?php
   $anotherVariable = 11;
   print $anotherVariable;
?>

This second example is almost identical to the first except that the variable is assigned an integer value rather than a string. The important thing to notice here is that when assigning an integer value, you do not use the quotation marks, whereas for a string, you place the text between quotes.

If you are familiar with other programming languages, you may think I was a bit sloppy and avoided declaring my variables to save some time and space. This is not the case! PHP is actually a “Loosely Typed Language”. What this means is that variables can be set without being declared; the variable is automatically declared when it is used.

On a final note, as with most programming languages, don’t forget that variables in PHP are case sensitve. $var1 & $Var1 are considered to be two completely different variables. Try and follow some sort of naming convention to avoid errors resulting from this sort of thing.

Turn your phpBB Forum into a Blog

Monday, May 14th, 2007

phpBB Blog 2.3 is a software add-on for phpBB which turns your forum into a blogging system. The latest version has been running stable now for over a year so it may be an interesting option to explore if you are interested in setting up a blog.

I’ve personally never tested this system so would be interested in getting reader feedback from anybody who has. I’ve personally always assumed it would not be as fully featured as other systems such as Wordpress which are also available for free and have thus avoided it. However, despite this I have recently heard a great deal of positive information about phpBB Blog so those hardcore phpBB fans out there may really be interested in trying out this sytem first.

The system works with phpBB 2. If anybody has tested with phpBB 3, please leave a comment to let us know if you encountered any problems.

Thanks!

Echo vs Print - Which Should be Used?

Friday, May 11th, 2007

You will notice as soon as you start programming in PHP that you have two options when it comes to displaying text on the screen: Echo or Print. Both examples shown below will display exactly the same text on the screen in exactly the same way. So… which one should you use?

<?php
   print "Hello World!";
?>

<?php
   echo "Hello World!";
?>

There is one small difference between print and echo. With print you can return a true or false value which means you can use it as part of a more complex expression. Because the print function gives you this extra functionality which may be of use, I would suggest simply using print all the time.

Of course, because echo does not return a value, it can be argued that echo is faster than print. This is true, though the difference in execution time is so small that in practice it is negligible. In the past, it may have been possible to speed up a large PHP application by several milliseconds by converting all prints to echos, but with the speed of today’s computers I doubt there would be any useful performance improvement. If you need to improve the performance of your program, optimizing your algorithms will be way more beneficial than switching to echo.

At the end of the day, it’s really up to the preference of the programmer when it comes to choosing between Echo and Print. As I’ve mentioned, I’ve gone with only using Print because I feel that my code is more easy to understand and therefore of higher quality this way than if I began mixing both Echo and Print. If I just used echo, I may still need to use Print in some cases should I need to return a value, therefore I would inevitably find myself mixing both functions and confusing any new PHP programmers I share my code with! Also, the print command is more common in other programming languages and is more intuitive which are some other reasons I have decide to stick with just using Print.

Here is an example of print in action:

<html>
<head>
   <title>PHP Page with Print in Action</title>
</head>
<body>
<?php
   print "This text will be displayed!";
   print "<br>";
   print "The line above used HTML!";
   print "This \"is how we can use\" quotes";
?>
</body>
</html>

How to Comment your PHP Code

Thursday, May 10th, 2007

Commenting your code is extremely important no matter which programming language you are using. Not only so others will be able to understand what you are doing, but so you yourself will be able to figure it out again quickly should you not look at your code for several months.

In PHP there are three ways you can comment your code:

<?php
   // This is a single line comment!
?>
<?php
   // # This is a single line comment as well!
?>
<?php
   /* This method is used for commenting
      larger blocks of text within the code.
      Everything between the start and end
      comment symbols will be a comment.
      This whole paragraph is therefore a
      comment.
   */
?>

Don’t fall into the bad habit of not commenting your code. While it may sometimes seem like a hassle and waste of time, you will actually find quit the opposite later on down the road. You will save time by being able to quickly re-use existing code and you will save co-workers and friends lots of frustration if you are working on a group project which may result in them buying you a nice coffee early in the morning from time to time! :)

Understanding PHP & the Web Server/Browser Relationship

Tuesday, May 8th, 2007

One of the tricky aspects of PHP is that each time you make any sort of request on the web page, the web browser will then open a connection to the web server, which will then process the request and send back a response. The connection to the server is then closed. This makes web programming different than other types of programming in the sense that you will need to use sessions in order to keep track of the state of the application. You will need to constantly reload and update these variables and pass them on as the user continues browsing the page. I’ll talk more about sessions and sessions variables later this week. The first thing that is important to understand is what is going on behind the scenes!

  1. First things first, the user opens up his/her browser. This you will soon find, is actually the source of much evil and many headaches as no one browser will interpret your web page the same.
  2. Next, the user types in the address to your website, or clicks a link to access your website. In so doing, a name server will then direct your browser to the server hosting your web page.
  3. The server hosting your web page will receive the request and retrieve the page that was requested by the browser.
  4. Since the page the server retrieves is a .php page, the server will compile the page using a just-in-time compiler that in turn generates the HTML code which is what is then sent back to the browser which made the initial request for the page.
  5. Lastly, the broswer receives the HTML code and displays its interpretation of it, which will often vary from browser to browser.

Users viewing your page will not be able to see your PHP code. This is because the server compiles the code and generates HTML which is sent back to the user. Because all of this takes place server-side, the user can only view the HTML source to your page.

Your First PHP Page

Thursday, April 19th, 2007

So far, I’ve introduced several open source applications built with PHP such as Joomla, PHP-Nuke and phpBB. I’ve also posted a brief introduction to PHP, which links to some useful tutorials and resources that will help you get PHP installed on your computer if you wish to test locally. All this information is a bit much to swallow all at once, so today, I want to get started right at the very basics and make a very simple PHP page.

There are tons of great tools available for developing code. My personal favorite is the most simple of all; Notepad! Notepad is a great tool for writing code because it doesn’t do anything for you. While this makes debugging difficult, it makes sure you learn your code better and understand what it is doing. I wouldn’t recommend Notepad for large projects, but for quick edits or simple pages, Notepad is a great tool which opens instantly and uses virtually zero resources.

Today we are going to write a simple program which displays, “Hello World!” on a website. Nothing too fancy, but it is the traditional starting point in just about every programming language I have tackled!

Open up Notepad:

<html> <head> <title >Hello World PHP Page!</title> </head> <body> <h1>Hello World PHP Page!</h1> <p> <?php echo "Hello World"; ?> </p> </body> <html>

Save the file with a .php extension. (ex: index.php or hello.php). Upload it to your web server and you are done!

Most of the above code is just basic HTML code for setting up a web-page. The PHP code begins with the “<?php” tag and ends with the “?>” tag. Everything between these tags is interpreted by PHP. “echo” is a simple PHP function which prints text to the screen. What you have therefore done with the simple code above it setup a web-page which includes PHP code to display the text, “Hello World!”. Try it out!

Introducing phpBB

Friday, April 13th, 2007

phpbb_logo.jpgphpBB is an extremely popular forum system written in PHP and available for free. There is no reason to ever pay for a forum system so long as phpBB is around, it is extremely powerful and offers all the same features as any commercial product. The latest version, called Olympus, has been available as a beta since June 2006 and supports things like sub-forums which were some of the lacking features.

An internet forum itself is a place where your users can hold discussions. This means users are constantly generating new content which will help increase visitor loyalty and traffic to your website. Having a forum on your website will help you create a community of users which is what you need as a solid foundation to help your website grow.

phpBB is also a great learning tool for those interested in learning PHP since it is an open source project. Browse through the code and see if you can make any changes to successfully customize your forum!

Introducing PHP-Nuke

Thursday, April 12th, 2007

nukebutton.jpg PHP-Nuke is a content managment system similar to Joomla, which gives the administrator the ability to easily post news and manage a community of users.

The latest version of PHP-Nuke (version 8.0) is available for $12, however, all previous versions (7.9 and earlier) are available for free and are still complete, fully functional systems.

In my opinion Joomla is currently a more professional, powerful and advanced system in comparison with PHP-Nuke. PHP-Nuke does however provide one significant advantage over Joomla; it comes packaged with phpBB! phpBB is a great forum system which is fully integrated into PHP-Nuke. Forum systems are available for Joomla, but must be installed separately as components and none are of as high quality as phpBB. There are ways to “bridge” phpBB with Joomla, however this is no simple task.

If you plan on running a community website which will have a high traffic forum, then PHP-Nuke is an excellent option for you! If you plan on running a large site based on content however, Joomla is much better at managing this.

About Web Dev Notes

Your one stop destination for anything and everything related to web development

Web Dev Notes Author(s)
    » Deceth

Blogging Flair

New Media, Web 2.0 Channel Posts

  • Podcasting Transcription
    A Guest Post from Tishia Lee of Tishia Saves Time: When I first started offering transcription services as part of my Virtual Assistant business, transcribing podcasts was not something I [...]
  • Don't Dabble - Make A Commitment
    If you want to get the best bang for your buck, plan on podcasting for the long haul. Podcasters who “test” things out with one or two podcasts and then give up may think that they've given [...]
  • A Question of Podcasting Frequency
    This follows up on yesterday's post about not dabbling in podcasting but rather to make a commitment. Podcasting on a schedule is important when building a community and an audience. Don’t have [...]
  • Do You Twitter About Your Podcast?
    Whenever I release a new episode of Work at Home Moms Talk Radio is pop the link up on my Twitter and invite my followers to come check it out. This has brought me several first time listeners which [...]
  • Looking for Podcast Outsourcing?
    I mentioned having my podcast audio's transcribed in my last post. In case you wonder - no, I do not transcribe them myself. (Shudder the thought - I did enough transcription to last a lifetime in [...]
  • Business Podcasting Benefit: Be An Industry Thought Leader
    I often ask people 'Why haven't you started your podcast yet?' and a common reply is 'I don't feel like I'm enough of an expert to cover the topic I'm interested in.' Enough of an expert? [...]
  • Business Podcasting Benefit: More Content
    Building a business on the web requires that you generate a lot of content. Content on your website draws traffic both through search engines (people searching for your content) and through links [...]
  • Business Podcasting Benefit: Deeper Relationships
    A website visitor is just a website visitor. You can't really say that you have a relationship with someone who has only visited your website can you? But when the web visitor subscribes to [...]
  • Business Podcasting Benefit: Increased Market Exposure
    When you consider how many millions of people are walking around with iPods and other Mp3 players you have to wonder, wouldn't it be awesome if they came looking for you? Well they do. Ipod [...]
  • D'ya Know Your Podcasting ABCs?
    I embarked on a fun little project this winter in which I have been working my way through the alphabet, looking for words for each letter that I could apply to the subject of podcasting. I've [...]

Hot Off The Press

  • Nom Nom Nom, yummy stuff, nom nom nom
    Technorati Tags: Wentworth Miller,Rob Lowe,Cam Gigandet,Justin Bruening,Dominic Purcell,Amaury Nolasco,Henry Simmons,Blair Underwood,Prison Break [...]
  • Smackdown! 10/10/08 - Videos
    SmackDown! 10/10/08 [...]
  • Ministry
    With so many options for ministry and reaching out to others with the Gospel; helping others with their physical needs, such as food and clothing, giving time to help at a nursing home, doing [...]
  • I Don't Like the Tone of Voice
    I don't like the tone of voice that my husband and I are raising our children in.  I know, that sentence shouldn't end with a preposition, but blah, this post may not have any punctuation.  [...]
  • Safe Halloween for Missouri Kids
    This year the children should be safe from sex offenders in their area, as long as the offenders actually abide by the law. A new law has come out and the offender MUST post a sign outside their home [...]
  • Fuckin Period
    Here is why having a period sucks. First of all, it is totally taboo to talk about in the public. I could be suffering and feeling ill from cramps, bloating, and any of the other symptoms - it [...]
  • Promo Images from Episode 3.07, “Eris Quod Sum”
    Here are several images from episode 3.07, “Eris Quod Sum.”  Kristen Bell fans will be pleased to see that she is back. Looks like we’ll be seeing a big fight between Elle and the [...]
  • My first post here where I go off on the husband...
    Ok, I want him to take some mental responsibility here.  I want him to take some of the grief out of my brain and carry it around in his brain for a little while.  Over 3 months ago I [...]
  • More Promotional Images from Episode 3.05, “Angels and Monsters”
    NBC has been busy releasing promo images for episodes 5, 6, and 7.  In this set, from episode 5, “Angels and Monsters,” we get our first look at villain Stephen Canfield, known as Vortex, as [...]
  • What's Up Austin: The Weekend Line-up for 10/11/08
    It looks like it's going to be a beautiful weekend to be out and about in our wonderful city. I don't know about you but I just love this time of year. The weather has cooled down and yet it's still [...]