Apply for the next session

Introduction

PHP turns into HTML.

That’s its job. It processes data and strings and numbers and it all ends up in a final HTML page – way before you ever see it in the browser.

OK OK – Not always. Sometimes you just run some logic on the server and there’s no HTML output. But definitely when we’re using it for templating. (which is what we’ll be using it for almost all of the time)

First things first

PHP is a server-side scripting language. Its primary purpose is to generate HTML. By the time you see the browser render the page, it’s rendering based on the HTML the PHP created (not the PHP).

Soak it in. Follow along.

PHP doesn’t actually know anything about HTML. It just sees strings of characters. (or at least that’s how you should think about it.)

You can output some strings – and if they are valid HTML, then good! If not… it won’t know / unless you get a fancy PHP-specific text editor and linter. (which we aren’t going to do in this course)

PHP allows you to save things in memory – and then create references (variables) to point to those things in memory so that you can reference them later in your program.

PHP runs top-down and executes in that order. If $name wasn’t initialized first, the program would break when it came upon a reference of $name (because it hadn’t been declared and cannot be found in memory)

This shows some strings and a variable being concatenated with the concatenation operator .

Interpolation. This is prettier, but it has to be dead simple. Double quotes are required.

https://stackoverflow.com/questions/4676417/should-i-use-curly-brackets-or-concatenate-variables-within-strings

Here’s some math. Note how the number has 3 decimal places. That’s not great for currency.

Here’s a useful built-in function. It has 4 parameters. The number to work with, the number of decimal places, the character to use as the decimal, and the character to visually denote breaks for thousands.

Try it out. With PHP, you have to put ALL of the parameters. See what happens if you leave one out!

This makes the money look nice. There are a great many built-in functions in the core PHP library. You can learn them in order of nessesity. If you want to do something there’s likely a function for that. Ask someone.

Things can get a little weird, so it’s up to you to choose how to compose things to make them the least weird. So, – maybe the dollar sign should be added in the $prettyTotal assignment. You could also write ${$prettyTotal} but that’s not much clearer.

If you’re going to do some work to format a number or something, maybe keep that in a single place. This way, if you need to change it, it’s easy to find.

Apply for the next session