This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
en:web_development:media:php [2021/12/20 17:36] mag created |
en:web_development:media:php [2023/08/16 09:33] (current) |
||
---|---|---|---|
Line 182: | Line 182: | ||
< | < | ||
* Change my name to yours and use this to see the code you're generating. | * Change my name to yours and use this to see the code you're generating. | ||
+ | * Of course, it's also possible to show your text on the screen, and that works well in early development, | ||
==== PHP For Loop ==== | ==== PHP For Loop ==== | ||
Line 194: | Line 195: | ||
?></ | ?></ | ||
* This should look very familiar. The only difference to Javascript is that our PHP variable begins with ' | * This should look very familiar. The only difference to Javascript is that our PHP variable begins with ' | ||
- | * Now we want to check that we are looping correctly. | + | * Now we want to check that we are looping correctly, so we'll use an error log. |
+ | * Add the following code inside the < | ||
+ | < | ||
+ | * If you refresh your page now then check your logs (< | ||
+ | * Note that < | ||
+ | * Now let's modify that line to show the image link. | ||
+ | < | ||
+ | * Refresh your page and check the logs again. You should see six lines with your name and the six different image links (or however many images you have added to your < | ||
+ | |||
+ | ==== PHP Strings ==== | ||
+ | * Before we go any further, let's take a closer look at strings in PHP, which are very similar to strings in Javascript. | ||
+ | * Let's start with some simple text. Add this just before your for loop. | ||
+ | < | ||
+ | * Refresh your page and see this text appears where your album should be. | ||
+ | * You might already have noticed one major difference between Javascript and PHP. In Javascript, to make your text appear in the document, you need to attach it to a particular element. In PHP, your text is entered wherever it appears in the flow of HTML code. In fact, it becomes part of the HTML code. | ||
+ | * Try this. Update your ' | ||
+ | < | ||
+ | * Now we've added HTML code for an image, so if you refresh your page you will see the first photo appear. | ||
+ | * Notice that use of < | ||
+ | * We want to change the image link to the data from our array, so we can break the code up as follows. | ||
+ | < | ||
+ | * Here we are replacing the image link with the first item in the photos array, and joining the parts of the string with < | ||
+ | * This works, but there is a cleaner way. Place the following code inside the for loop. | ||
+ | < | ||
+ | * When you refresh your page you should see this sentence 6 times exactly as is. That's not what we want. Let's change it a bit. | ||
+ | < | ||
+ | * Now when you refresh your page you should see the sentence 6 times with the numbers 0 to 5, which is much better. | ||
+ | * So wrapping the string in < | ||
+ | * Let's try that with the array item. | ||
+ | < | ||
+ | * Refresh the page and see that we now see the string. | ||
+ | * Let's change this to the HTML for our image. | ||
+ | < | ||
+ | * Refresh the page and you should now see your photos displayed properly. | ||
+ | * We could stop here, but I want to take it one step further. | ||
+ | * In your page, right click and select 'View Page Source' | ||
+ | * Notice how most of the page is readable, but the code generated by PHP (the list of images inside < | ||
+ | < | ||
+ | * Refresh your page and check the source code again. Now it's much better. | ||
+ | * We've added < | ||
+ | * Now you're ready to practise. | ||
+ | |||
+ | [[en: |