Difference between revisions of "PHP103"
(17 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
+ | [[Main Page]] >> [[CP2228|Web Application Development]] >> [[PHP|Workbook]] >> PHP Basics | ||
+ | |||
== PHP Start Symbols == | == PHP Start Symbols == | ||
<nowiki> | <nowiki> | ||
− | |||
<?php | <?php | ||
</nowiki> | </nowiki> | ||
− | |||
− | |||
== PHP End Symbols == | == PHP End Symbols == | ||
Line 16: | Line 15: | ||
== Commenting in PHP == | == Commenting in PHP == | ||
<nowiki> | <nowiki> | ||
− | <? | + | <?php |
echo 'This would be printed out'; | echo 'This would be printed out'; | ||
Line 36: | Line 35: | ||
<nowiki> | <nowiki> | ||
− | <? | + | <?php |
echo 'Hello'; | echo 'Hello'; | ||
echo "World"; | echo "World"; | ||
Line 53: | Line 52: | ||
If you try and do this: | If you try and do this: | ||
<nowiki> | <nowiki> | ||
− | <? | + | <?php |
− | echo "My name is " | + | echo "My name is "Alix" - yes it is!"; |
?> | ?> | ||
</nowiki> | </nowiki> | ||
You would probably expect the output to be: | You would probably expect the output to be: | ||
<nowiki> | <nowiki> | ||
− | My name is " | + | My name is "Alix" - yes it is! |
</nowiki> | </nowiki> | ||
But what you will actually get is something like: | But what you will actually get is something like: | ||
− | + | https://mi-linux.wlv.ac.uk/wiki-images/PHP103-01.png | |
To output speech marks, either single (') or double (") in echo or print statements you must ESCAPE them | To output speech marks, either single (') or double (") in echo or print statements you must ESCAPE them | ||
Line 73: | Line 72: | ||
So the example in the previous section becomes: | So the example in the previous section becomes: | ||
<nowiki> | <nowiki> | ||
− | <? | + | <?php |
− | echo "My name is \" | + | echo "My name is \"Alix\" - oh yes it is!"; |
?> | ?> | ||
</nowiki> | </nowiki> | ||
Line 80: | Line 79: | ||
And the output you get is: | And the output you get is: | ||
<nowiki> | <nowiki> | ||
− | My name is " | + | My name is "Alix" - oh yes it is! |
</nowiki> | </nowiki> | ||
+ | |||
+ | == Ready to move on? == | ||
+ | [[PHP104|PHP104 - PHP Variables]] |
Latest revision as of 10:20, 20 September 2023
Main Page >> Web Application Development >> Workbook >> PHP Basics
PHP Start Symbols
<?php
PHP End Symbols
?>
- these symbols indicate the end of a section of PHP code
Commenting in PHP
<?php echo 'This would be printed out'; // THIS IS A SINGLE LINE COMMENT AND WON'T BE PRINTED # BELIEVE IT OR NOT, THIS IS ALSO A SINGLE LINE COMMENT /* THIS SYMBOL PAIR STARTS A MULTI-LINE COMMENT BLOCK THAT ONLY ENDS WHEN YOU GET TO THE FOLLOWING SYMBOL PAIR */ echo 'This would also be printed out'; ?>
- as shown above, there are a number of ways to include comments in PHP code
Outputing in PHP
There are essentially two ways of outputting in PHP - echo and print - there is very little difference between the two - at the high-performance end of PHP, echo is fractionally faster, however print can be used in complex expressions - for the basic level of PHP there is no difference.
<?php echo 'Hello'; echo "World"; print 'Where are'; print "you?"; ?>
- all of the above are valid examples of outputting
Which symbol to use ' or "?
Again, these are essentially the same.
How do I print a ' or " in my output?
This is a very important note, that often causes problems for the novice PHP programmer.
If you try and do this:
<?php echo "My name is "Alix" - yes it is!"; ?>
You would probably expect the output to be:
My name is "Alix" - yes it is!
But what you will actually get is something like:
To output speech marks, either single (') or double (") in echo or print statements you must ESCAPE them
Escaping Speech Marks
To ESCAPE speech marks, you put a backslash symbol only in front of the ones you want to print out.
So the example in the previous section becomes:
<?php echo "My name is \"Alix\" - oh yes it is!"; ?>
And the output you get is:
My name is "Alix" - oh yes it is!