IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
To/From Code
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesPerlLanguage Basics  Print This     

Cross Ref > Language Basics

By Mike Prestwood

Perl versus PHP: A side by side comparison between Perl and PHP.

 
Language Basics
 

Language basics is kind of a catch all for absolute beginner stuff. The items (common names) I chose for language basics is a bit random and include items like case sensitivity, commenting, declaring variables, etc.

Case Sensitivity

[Other Languages] 

Languages Focus

Case sensitiviy in this case is referring to commands and variable names. For example, are "printf" and "PrintF" equivalent? Are fullname and FullName equivalent? When you create commands, operations, methods, or variables should you worry about case?

Perl:   Yes

Perl is case sensitive.

Syntax Example:
print "hello"; //This works.
Print "hello"; //This does not.
PHP:   Yes and No

PHP is case sensitive with variable names but not with commands. Although commands are case incenstive, I prefer to use all lowercase because it's easy to type and that's what I see most PHP coders doing and I see it on PHP.Net.

Syntax Example:

All of the following are equivalent:

echo "hello<br>";
ECHO "hello<br>";
Echo "hello<br>";
eCHo "hello<br>";

...but variables are case sensitive:

$fullname = "Mike Prestwood"; //These are two...
$FullName = "Wes Peterson";   //separate varialbes.




Code Blocks

[Other Languages] 

Languages Focus

The rules for code blocks within a language dictate where you can declare variables, how you "bracket" code, etc.

Perl: 

In Perl, you create the entire HTML page within your .PL script file using print commands.

For Perl, PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in this example because I see morePeal codeformatted that way.

Syntax Example:
$x = "Yes";
 
If ($x == "Yes") {
print "Hello world";
  print "I am a Perl coder.";
}
PHP:   { }

In .PHP html pages, you embed PHP code between <?PHP and ?>.

For PHP, JavaScript, Java,and C++, I prefer to put the first { at the end of the first line of the code block as in the example above because I see morePHP codeformatted that way (and on PHP.Net).

PHP Alternative Syntax

Although I don't like to use it, PHP offers an alternative syntax for if, while, for, foreach, and switch. These code blocks are surrounded by statement ending keywords that all use End with camel caps such as endif, endwhile, endfor, endforeach,and endswitch.

Syntax Example:
<?PHP
$x = "Yes";
//Simple if
If ($x == "Yes")
echo "hello world";
 
//If with a block of code.
If ($x == "Yes") {
echo "Hello world";
  echo "I am a PHP coder!";
}
?>




Comments

[Other Languages] 

Languages Focus

Commenting code generally has three purposes: to document your code, for psuedo coding prior to coding, and to embed compiler directives. Most languages support both a single line comment and a multiple line comment. Some languages also use comments to give instructions to the compiler or interpreter.

Perl:   #

Commenting Code
Perl uses # for single line comments and Perl does NOT have a multiple line comment.

Compiler Directives (A special comment.)

Perl also uses compiler directives embedded in comments with #! as in:

#!/usr/local/bin/perl -w
Syntax Example:
#This is a comment in Perl.
PHP:   # or // or /* ... */

Commenting Code
Use the multi-line to comment out large blocks of code and to write multiple line comments.

Syntax Example:
#This is a comment in PHP.

//This is too!

/*
This is a multi-line
comment.
*/




Constants

[Other Languages] 

General Info: Computer Language Constants

A constant is just like a variable (it holds a value) but, unlike a variable, you cannot change the value of a constant.

Perl:   use constant

In Perl, you declare constants using the use constant keywords:

use constant CONST_NAME => "Value";

Constants in Perl are case sensitive. A common standard in Perl is to use all-uppercase letters, with underscores to separate words within the name.

Syntax Example:
use constant FULL_NAME => 'Mike Prestwood';
use constant AGE => 38;
  
print "Your name is " . FULL_NAME . ".<br>";
print "You are " . AGE . ".<br>";
PHP:   define

In PHP, you declare constants using the define keyword:

define("CONST_NAME", "Value");

Constants in PHP are case sensitive. A common standard in PHP is to use all-uppercase letters, with underscores to separate words within the name.

Syntax Example:
define('FULL_NAME', 'Mike Prestwood');
define("AGE", 25);
  
echo "Your name is " . FULL_NAME . ".";
echo "You are " . AGE . ".";




End of Statement

[Other Languages] 

Languages Focus

In coding languages, common End of statement specifiers include a semicolon and return (others exist too). Also of concern when studying a language is can you put two statements on a single code line and can you break a single statement into two or more code lines.

Perl:   ;
Syntax Example:
print "Hello";
PHP:   ;
Syntax Example:
echo "Hello";




Literals

[Other Languages] 

General Info: Programming Literals

A value directly written into the source code of a computer program (as opposed to an identifier like a variable or constant). Literals cannot be changed. Common types of literals include string literals, floating point literals, integer literals, and hexidemal literals. Literal strings are usually either quoted (") or use an apostrophe (') which is often referred to as a single quote. Sometimes quotes are inaccurately referred to as double quotes.

Languages Focus

In addition to understanding whether to use a quote or apostrophe for string literals, you also want to know how to specify and work with other types of literals including floating point literals. Some compilers allow leading and trailing decimals (.1 + .1), while some require a leading or trailing 0 as in (0.1 + 0.1). Also, because floating point literals are difficult for compilers to represent accurately, you need to understand how the compiler handles them and how to use rounding and trimming commands correctly for the nature of the project your are coding.

Perl:   quote

String literals are quoted as in "Prestwood". If you need to embed a quote use a slash in front of the quote as in \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
print "Hello";
print "Hello \"Mike\".";
  
#Does Perl evaluate this simple
#floating point math correctly? No! 
if ((.1 + .1 + .1) == .3) {
  print("Correct");
} else {
  print("Not correct");
}
PHP:   quote or apostrophe

In PHP you can use quotes, or apostrophes as in "Prestwood", and 'Prestwood' for string literals. Use a slash in front of a quote or apostrophe to embed same type as in \' and \".

To specify a floating point literal between 1 and -1, you can preceed the decimal with a 0 or not (both work). In other words, preceding and following decimals are allowed (both .1 and 0.1). Trailing decimals are also allowed (1, 1., and 1.0 are all equivalent and allowed).

Syntax Example:
echo "Mike's drums are over there.<br>";
echo 'Mike said, "hi!"<br>';
  
//Does PHP evaluate this simple
//floating point math correctly? No! 
If ((.1 + .1 + .1) == .3) {
 Echo "Correct";
} Else {
 Echo "Not correct";
}




Variables

[Other Languages] 

Languages Focus

A variable holds a value that you can use and change throughout your code so long as the variable is within scope. With variable declaration, you not only want to know the syntax of how you declare a variable but you also want to know where. Are you allowed to declare a variable inline? What are the available scopes: local vs. global. Can you assign a value at the same time you declare a variable?

Perl:   $x = 0;

Perl is a loosely typed language with only three types of variables: scalars, arrays, and hashes. Use $ for a scalar variable, @ for an array, or % for a hash (an associative array).

The scalar variable type is used for any type of simple data such as strings, integers, and numbers. In Perl, you identify and use a variable with a $ even within strings.

Syntax Example:
#!/usr/local/bin/perl -w
 
print("Content-type: text/html\n\n");

$fullname = 'Mike Prestwood';
$Age = 38;
$Weight = 162.4;
 

print "Your name is $fullname.
";
print "You are $Age and weigh $Weight.
";
PHP:   $x = 0;

PHP is a loosely typed language. No variable types in PHP. Declaring and using variables are a bit different than in other languages. In PHP, you identify and use a variable with a $ even within strings!

You assign by reference with & as in &$MyVar.

Syntax Example:
$fullname = 'Mike Prestwood';
$FullName = 'Wes Peterson'; //This is a different variable!
$Age = 38;
$Weight = 162.4;
 

echo "Your name is $fullname.
";
echo "You are $Age and weigh $Weight.
";




Sales Website: www.prestwood.com Or visit our legacy sales site: 
legacy.prestwood.com


©1995-2025 Prestwood IT Solutions.   [Security & Privacy]