|
Fusebox Migration: From ColdFusion to PHP
November 18, 2001
|
::: The Basics: setting variables :::
|
ColdFusion puts everything into tags*. You set variables, include other files, and even delimit conditional blocks using tags.
In PHP, tags are left to the HTML. Instead we have to do our coding within script delimiters much like JavaScript or ASP. In PHP, these delimiters look like this:
| |
<?php
//code goes in here...
?>
|
From within those delimiters, however, we can do everything PHP allows. Let's start by setting a variable:
In ColdFusion:
In PHP:
You may have noticed the "$" (dollar sign) in front of the variable name. In PHP that is the way the PHP engine differentiates a variable from the rest of the code. There is also a ";" (semicolon) at the end of the line. That tells the PHP engine that this is the end of a line (;-D). This is just like JavaScript, though PHP is a little more strict about the presence of that ";".
In ColdFusion, you can also conditionally set variables using CFParam. Like this:
In ColdFusion:
| |
<cfparam name="foo" default="bar">
|
In PHP:
| |
<?php
if(!isset($foo)) { $foo = "bar"; }
?>
|
isset is the equivalent of ColdFusion's IsDefined: it just asks if the variable has been set yet. The "!" (exclamation point) is the equivalent of ColdFusion's NOT operator: it essentially inverts the result of a boolean expression. So the PHP phrase basically says, "if $foo is not set, then set $foo equal to 'bar'.". In ColdFusion, the same can be written this way (and this is also equivalent to CFParam):
| |
<cfif NOT IsDefined(foo)>
<cfset foo="bar">
</cfif>
|
|