Lesson 2: PHP Comments and PHP Variables

 

I’m getting feedback that you want more lessons. That was just the motivation I needed :-) .

Getting Started
If you’re just joining us, you may need to review some of the material we’ve already covered
1) Lesson 1: Using PHP for the first time
2) Comment Out PHP

Defining Variables
Variables are useful for storing information, especially dynamic information. If you have a quantity that might “vary” you probably want to make it a variable. To create a variable in PHP you simply have to put a dollar sign just before the variable’s name. For example: $my_variable.

Learning by example
Take a look at the following example:

<?php
  // This is a comment: Let's define our first variable.
  $i_am_a_variable = 5;
 
  // Let's add one to the variable and print it to the screen
  print $i_am_a_variable + 1;
?>

Copy and paste the above code into codepad as discussed in Lesson 1. If you execute the code in codepad you will find the result is 6.

Woohoo! Now that’s some real coding there. Hopefully, you’re beginning to see the power of PHP.

Some notes about variables

  • Variables must be made up of letters (lower case or upper case) and numbers
  • You may use underscores “_” too (look at the code above for an example)
  • Variable names can’t start with a number, so don’t name your variable something like $789 or $8my_dinner_today!
  • If you’re not a programmer don’t read on: PHP is a loosly typed language so PHP variables don’t need to be defined as a string, int, etc.

Back to Learn PHP in 24 Hours for more fun :-)

 

Learn PHP faster. Buy now!
 

Lesson 1: PHP Basics (PHP Hello World)

 

Do you want to learn php in 24 hours? 

The journey starts with the first step.  When most programmers start learning a new language they start with what is called a “Hello, World!” program.  This is a simple program with the goal of printing the text “Hello, World!” to the screen.

While the most useful way to write the “Hello, World!” program might be for a real web page, let’s start even simpler.  Go to codepad and select the radio button for “PHP”.

Paste the following code into the codepad window and click submit:

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

In the output section of codepad you should see Hello, World!. Congratulations! You’ve just written your first php snippet.

Code Review

PHP always starts and ends with <?php and ?> respectively. This is used to tell the interpreter this is PHP as opposed to, say, HTML.

The rest is more straight forward. The print command prints the text in quotes. Finally every command must be followed by a ;. This allows you to write a multi-line command without confusing PHP: PHP will still know where the end of the line is!

Back to Learn PHP in 24 Hours for more fun :-)

 

Learn PHP faster. Buy now!
 

Online PHP Interpreter

 

It’s pretty easy to test PHP code. However, sometimes you just need to validate a short snippet of PHP.

For example, here at Learn PHP in 24 Hours, we’re constantly writing up short PHP examples. Naturally it’s important they actually work and testing the snippet can feel a little cumbersome considering you may only need to validate a line or two of code.

So whether you’re just learning PHP and want to test out your new PHP skills or you just need to validate a snippet of code, there is a solution:

Check out codepad! codepad will interpret your PHP code on the fly. All you need is a browser and a web connection. You can also test Ruby, Perl, C++, etc.

Figure 1. codepad in action

Back to Learn PHP in 24 Hours for more fun :-)

 
 

PHP Modulo Tutorial

 

Modulo math is very important to many fields of study. For example, we perform modulo 12 arithmatic any time we read an analog clock.

According to Wikipedia:

In mathematics, modular arithmetic (sometimes called clock arithmetic) is a system of arithmetic for integers, where numbers “wrap around” after they reach a certain value—the modulus.

PHP modulo arithmatic isn’t hard but it’s something you need in your set of tools as a PHP programmer.  You might use it to check if a number is odd or even, to create a series of numbers that repeats (0, 1, 2, 0, 1, 2…), to find the remainder from a division problem, etc.

PHP uses the percent sign (%) to perform modulo arithmatic.  For example:

<?php
echo 4 % 12 . "\n";
echo 12 % 12 . "\n";
echo 13 % 12 . "\n";
echo 13.21 % 12.1 . "\n";
echo -13 % 12 . "\n";
?>

will result in:

4
0
1
1
-1

Note that:

  • Decimal values are stripped down to integers (13.6 becomes 13 for example)
  • Negative values are taken into account (be careful not to forget…)

Back to Learn PHP in 24 Hours for more fun :-)

 
 

Comment Out PHP

 

Comments are an important part of any programming language and PHP is no exception. Comments are used to document code (for yourself and/or others) and to comment out php as needed. PHP provides two forms of inline comments and one form for multiline comments.

PHP Comment Types by Example:

<?php
echo "There are comments below but you won't see them.";
// A double forward slash comments out just this line.
# A single pound sign comments out just this line.

echo "and how about a multi-line comment?";
/*
Here is how
you can 
comment out php
on several lines
*/
?>

The above code fragment when interpreted would display:

There are comments below but you won't see them.
and how about a multi-line comment?

Well that’s about all there is to know about comments.

Back to Learn PHP in 24 Hours for more fun :-)

 
 

PHP instr() tutorial

 

instr is a function used to find the first occurrence of a sub-string within a larger string.

Unlike ASP or VB, PHP does not use the terminology instr, but instead uses strpos.  Although named differently, strpos has the same basic functionality as instr.

strpos syntax:

int strpos (string $string1, mixed $string2 [, int $start = 0])

string1

String to search in

string2

The sub-string to find within string1

start

Optional.  This is the character position to start at.  This defaults to the first character if omitted (start = 0 by default).

Return value:

Returns the integer position of the first character withing string1. FALSE is returned if string2 is not found within string1.

What about some examples then?

$position = strpos("this is my example", "is");

will return the integer value 2.

Whereas,

$position = strpos("this is my example", "is", 3);

will return the integer value 5.

Other related functions you might enjoy!

 
strrpos() - Find position of last occurrence of a char in a string
stripos() - Find position of first occurrence of a case-insensitive string
strripos() – Find position of last occurrence of a case-insensitive string in a string
strrchr() - Find the last occurrence of a character in a string
substr() - Return part of a string
stristr() - Case-insensitive strstr
strstr() - Find first occurrence of a string

(function list from php.net)

Back to Learn PHP in 24 Hours for more fun :-)

 
 

You can learn php in less than 24 hours!

 
  • Do you want to take your web skills to the next level?
  • Do you want to make the most of your time?
  • Are you tired of paying someone else to install and write your php scripts?

Discover how easy it is to master PHP using Robert Plank’s SimplePHP eBook.

Learn PHP in 24 hours or less! Don’t miss this opportunity, invest in yourself now:  Click Here!


Buy Now!

Back to Learn PHP in 24 Hours for more fun :-)

 
 

Hello, learn php in 24 hours .org!

 

Welcome, and thanks for visiting.  This is a test post and the first post to this site.

For more information check out the about page.

Back to Learn PHP in 24 Hours for more fun :-)