PHP for Xojo Programmers - 0001 - Hello World

By: | On:


Reposted from INN:

This is the first post in a series about PHP from a Xojo perspective.

I'll be showing how I developed different parts in detail of Xanadu which is open source:

Hello World!

When I first used Xojo I did the same thing you probably did. I dragged out a button and when clicked it showed a message box that just said 'Hello World'.

Think about what Xojo might be doing to make this work.

  • Define the page, even if blank
  • Define and place a button
  • Add a button onclick event that shows a dialog 'Hello World'.

Hello World - Without PHP

Interestingly, you don't need PHP for this but you do need HTML, CSS, and Javascript. However, adding PHP brings in the power of programming. In my head, when I use Xojo I'm dragging buttons and adding code to write the HTML, CSS, and Javascript for me. PHP is what I now use to write HTML, CSS, and Javascript. Xojo tries hard to not expose the details but you can see a bit when folks share Javascript 'work arounds'. The key with PHP is to understand the details then write PHP code to mange the details.

Define the page


That's it. We now have a blank page. The html and body tags are all you need at first. The files should be saved with an html extension like index.html or index.htm.

Define and place a button

<button type="button">Hello</button>

Buttons are just another tag, but have attributes or properties. Note [type="button"] and a button label [Hello]

Add a button onclick event that shows a dialog 'Hello World'

<button type="button" onclick="alert('Hello World!');">Hello</button>

Another Button attribute is onclick which runs the Javascript [alert('Hello World!');] The alert function shows a dialog with the text [Hello World!]

Hello World - With PHP

Now we can add some code to manipulate the HTML and Javascript.

PHP Tags

// This is a comment
<button type="button" onclick="alert('Hello World!');">Hello</button>

Now we have a PHP tag with a comment, but it doesn't do anything yet. The file extension should be changed to php like index.php.

Add a Variable

$message = 'Hello World';
<button type="button" onclick="alert('<?= $message ?>');">Hello</button>

Not so bad right? First we define a variable at the top of the page wrapped in a PHP tag. Then in the Javascript alert function we add a special PHP tag that is a shortcut to echo the variable value. another way would be to use the longer PHP tag.

<?php echo $message; ?>

Add a Variable with Javascript

$buttonLabel = 'Hello';
$buttonJS = "alert('Hello World!');";
<button type="button" onclick="<?= $buttonJS ?>"><?= $buttonLabel ?></button>

Here we create and use a variable for the button label and the button onclick javascript. Notice that we used single quotes for the first variable and double quotes for the second variable. Quoting can get tricky with PHP quotes, HTML quotes and Javascript Quotes.


Next time you're using Xojo think about how you might do it in PHP. Keep in mind when I say PHP, I really mean using PHP to write HTML, CSS, and Javascript...

Ask Questions

Ask questions below. Some questions might end up being answered in another post. :)


CampSoftware Blog:

Xanadu Info:

Xanadu Download: