Software Development Lesson #2

Please read lesson #1 here before proceeding. You must install and set up xampp before continuing.

Now begins lesson two. Create a file and put it in the same directory as helloworld.php in the previous lesson. Make sure xampp is turned on as well. Enter the following code and run it in your browser, as in the previous lesson:

<?php

$var = "Durk is a sex god";

echo $var;

?>

In your browser you should see a page with the words 'Durk is a sex god'. $var is a variable, and it is assigned to the value 'Durk is a sex god'. In PHP, variables must begin with the dollar sign '$'. 'Durk is a sex god' is an example of a string. Other strings would be 'Montana', 'snow1234', and 'lol wut'.

Alternatively, we could have assigned $var to a number. When you assign a number to a variable, you do not use quotes as you do with text strings. For instance, $speed = 45. Reason is, when you use quotes, you're saying that the variable is a text string and not a number.

At any rate, you can perform the usual operations on numbers: addition, subtraction, multiplication, division, power, etc. You can also concatenate strings by using the + operator. Hence, you can add 45 + 5 and get 50. "This is" + "a sentence" yields "This is a sentence". Also, "45" + "5" is "455" because the quotation marks make them strings and not numbers.

Also, when you assign values to variables, the variable must go on the left side of the = sign, and the value you're assigning it to goes on the right. You can also do things like $nmbr = $nmbr + 2, which takes the variable $nmbr, and increments it by 2.

----

The assignment associated with this lesson is as follows. Consider the equation distance = velocity * time. Write a program that creates variables for each of those three values, and calculates distance from your velocity and time variables. Now, concatenate the string "miles" to the end of your distance value, and echo the result.
 
Back
Top