PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO). Sergey D Skudaev. Читать онлайн. Newlib. NEWLIB.NET

Автор: Sergey D Skudaev
Издательство: Издательские решения
Серия:
Жанр произведения: Компьютеры: прочее
Год издания: 0
isbn: 9785449090157
Скачать книгу
3; Division-assignment: $a equals $a divided by 3.

      $a.= 6; Concatenation-assignment: $a equals $a concatenated

      with 6

      Let’s write a simple code converting Fahrenheit to Celsius.

      <?php

      // $F is a temperature in Fahrenheit, $C is in Celsius

      $F=80;

      //The “echo” is a command that Output: s text to

      //the browser

      echo “F=”. $F.”<br>”;

      // a formula to convert Fahrenheit to Celsius

      $C = ($F – 32) * 5/9;

      //Output: temperature in Celsius

      echo “C=”. $C.”<br>”;

      ?>

      Output:

      F=80

      C=26.6666666667

      Please pay attention to “F=”. $F.’<br>‘on the code line echo; Here we concatenate 3 separate strings into one.

      “F=”

      $F

      “<br>" – HTML break.

      Two dots are placed between our three strings because dots are used to concatenate strings in PHP:

      “F=”. $F. "<br>”;

      The fact is that $F is not a string. It is an integer because it is equal to 80. If $F were equal to “80” it would be a string.

      When we concatenate the $F variable between 2 strings, because PHP converts it to a string, there is no need to do anything further.

      As I wrote earlier, a string of text can be enclosed in single quotation marks:

      echo “F=”. $F.”<br>”;

      To Output: a string of text, you can use the “print” command instead of the “echo” command.

      However, in this case, you would use parentheses and single or double quotation marks.

      print (“F=”. $F.”<br />”);

      Or

      print (“F=”. $F.”<br />”);

      PHP variables are case sensitive, so $F is not the same as $f, and $lastname is not the same as $Lastname or $LASTNAME.

      Let us say that we wish to use PHP to Output: a web page to a browser using the following HTML link:

      <a href="http://www.yahoo.com">yahoo</a>

      If we write

      print (”<a href="http://www.yahoo.com">yahoo</a>");

      we would receive an error message due to the use of double quotation marks in our HTML code. To avoid the error message, use single quotation marks for the print command as I’ve done in the following:

      print (”<a href="http://www.yahoo.com">yanhoo</a>');

      The same is true for an HTML input element:

      echo '<input type=“text” name=“lname” value=“James”/>”;

      or

      print (”<input type=“text” name=“fname” value=“John”/>”);

      One handy feature of the PHP code is the ability to include one file inside another. The creation of a website solely in HTML requires you to edit every single page of that website if you later wish to change the navigation, logo or footer. The use of PHP to create the web pages allows you to include one file with navigation or logo in every web page requiring you to edit only one file to later change the navigation or logo.

      There are two commands to include the file in your PHP code:

      include('top_menu.php’);

      or

      require("footer.php”);

      or

      require_once ("footer.php”);

      The difference between the two is, when using the “include” command, if the file to be included does not exist, you will be given a warning message; however, the execution of the rest of the PHP code will not be interrupted. Conversely, when using the “required” command, if the required file is not found, a fatal error message is displayed, and the execution of the PHP code will cease. The require_once is similar to the required?? but it checks to include the footer.php file only once even if you write require_once ("footer.php”); twice.

      Function

      A function is a piece of code that performs a specific manipulation with variables and then returns the result from that manipulation. Functions are useful because they can be used multiple times over. There are PHP functions that are built-in, but a function can also be created by the user. Let’s create one that converts Fahrenheit to Celsius. The PHP function declaration begins with the word function to be followed by the function name, which should be one word. Several parameters can be given to a function, but a function can only return one parameter in the return statement. Multiple parameters are enclosed together in parentheses and separated by comas. The entire piece of code inside a function is enclosed in curly brackets.

      In our example, we pass one parameter, a temperature in Fahrenheit, and return a temperature in Celsius.

      function convertFtoC ($F) {

      $C = round (($F – 32) * 5/9);

      return $C;

      }

      Now to convert Fahrenheit to Celsius you must call your function and pass the temperature in Fahrenheit.

      $T= convertFtoC (80);

      echo $T.”<br>”;

      The Output: will be 27

      The round is a built-in PHP function. It will round 26.6666666667 to 27.

      Variable Scope

      Variables in the PHP code may have different scopes. They may be seen or accessed globally, anywhere on the current php page or only inside the function. Let us analyze the following code:

      <?php

      $greeting=“Hello!”;

      function greetme ($name) {

      echo $greeting.””. $name;

      }

      //function call

      greetme (“John’);

      ?>

      Output: John

      At the beginning of the code, we assigned the “Hello” string to the $greeting variable. Then we defined the function with one parameter $name. Inside the function we concatenated the $name variable to the $greeting variable. Then we called the function and passed the name “John’ as a parameter.

      You might expect the Output: would be “Hello John”, since $greeting equals “Hello”, but it’s “John”. “Hello” is not visible because the $greeting variable inside the function is not the same as outside. It’s local, and because we assigned no value to the local variable $greeting, the value we passed as a parameter is the only one visible.

      Let us modify the function.

      <?php

      $greeting=“Good morning!”;

      function