Friday, June 21, 2013

PHP - Echo

  1. As you saw in the previous lesson, the PHP function echo
  2. is a means of outputting text to the web
  3. browser. Throughout your
  4. PHP career you will be using the echo function more than any other.
  5. So
  6. let's give it a solid perusal!
  7. Outputting a String
  8. To output a string, like we have done in previous lessons, use
  9.  the PHP echo function. You can
  10. place either a string variable or you
  11.  can use quotes, like we do below, to create a string that the echo
  12. function
  13.  will output.
  14. PHP Code:
  15. Code:
  16. <?php
  17. $myString = "Hello!";
  18. echo $myString;
  19. echo "<h5>I love using PHP!</h5>";
  20. ?>
  21. Display
  22. :
  23. In the above example we output "Hello!" without a hitch. The text we are
  24. outputting is being sent
  25. to the user in the form of a web page, so it is important that we use proper
  26. HTML syntax!
  27. In our second echo statement we use echo to write a valid Header 5 HTML statement.
  28. To do this
  29. we simply put the <h5> at the beginning of the string and closed it at the end of the
  30. string. Just
  31. because you're using PHP to make web pages does not mean you can forget
  32. about HTML syntax!
  33. Careful When Echoing Quotes!
  34. It is pretty cool that you can output HTML with PHP. However, you must be careful when
  35. using HTML code or
  36. any other string that includes quotes! The echo function uses
  37. quotes to define
  38. the beginning and end of the string, so you must use one of the following
  39. tactics if your string contains
  40. quotations:
  41. .:. Don't use quotes inside your string
  42. .:. Escape your quotes that are within the string with a backslash. To escape a quote
  43. just place a
  44. backslash directly before the quotation mark, i.e. \"
  45. .:. Use single quotes (apostrophes) for quotes inside your string.
  46. See our example below for the right and wrong use of the echo
  47. function:
  48. PHP Code:
  49. Code:
  50. <?php
  51. // This won't work because of the quotes around specialH5!
  52. echo "<h5 class="specialH5">I love using PHP!</h5>";
  53. // OK because we escaped the quotes!
  54. echo "<h5 class=\"specialH5\">I love using PHP!</h5>";
  55. // OK because we used an apostrophe '
  56. echo "<h5 class='specialH5'>I love using PHP!</h5>";
  57. ?>
  58. If you want to output a string that includes quotations, either use an apostrophe ( ' ) or escape
  59. the
  60. quotations by placing a backslash in front of it ( \" ). The backslash will tell PHP that you want the
  61. quotation
  62. to be used within the string and NOT to be used to end echo's string.
  63. Echoing Variables
  64. Echoing variables is very easy. The PHP developers put in some extra work
  65. to make the common
  66. task of echoing all variables nearly foolproof! No quotations are
  67. required, even if the variable does
  68. not hold a string. Below is the correct
  69. format for echoing a variable.
  70. PHP Code:
  71. Code:
  72. <?php
  73. $my_string = "Hello Bob. My name is: ";
  74. $my_number = 4;
  75. $my_letter = a;
  76. echo $my_string;
  77. echo $my_number;
  78. echo $my_letter;
  79. ?>
  80. Display:
  81. Hello Bob. My name is: 4a
  82. Echoing Variables and Text Strings
  83. You can also combine text strings and variables. By doing such
  84. a conjunction you save yourself
  85. from having to do a large number of echo
  86. statements. Variables and text strings are joined together
  87. with a period( . ). The example
  88. below shows how to do such a combination.
  89. PHP Code:
  90. Code:
  91. <?php
  92. $my_string = "Hello Bob. My name is: ";
  93. $newline = "<br />";
  94. echo $my_string."Bobettta".$newline;
  95. echo "Hi, I'm Bob. Who are you? ".$my_string.$newline;
  96. echo "Hi, I'm Bob. Who are you? ".$my_string."Bobetta";
  97. ?>
  98. Display:
  99. Hello Bob. My name is: Bobetta
  100. Hi, I'm Bob. Who are you? Hello Bob. My name is:
  101. Hi, I'm Bob. Who are you? Hello Bob. My name is: Bobetta
  102. This combination
  103. can be done multiple times, as the example shows. This method of joining
  104. two or
  105. more strings together is called concatenation and we will talk more about this
  106. and other forms of
  107. string manipulation in our string lesson.

No comments:

Post a Comment