Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO)
Sergey D Skudaev
© Sergey D Skudaev, 2018
ISBN 978-5-4490-9015-7
Created with Ridero smart publishing system
Introduction
DISCLAIMER
The information presented in this eBook is sold without warranty either expressed or implied. The author will hold no liability for any damages caused either directly or indirectly by the instructions contained in this book, or by the software products recommended for installation and usage.
Who Should Read This Book?
Those who wish to learn computer programming in PHP should be familiar with HTML, as a knowledge of HTML will be helpful in your efforts. This book is also for those who know other programming languages and wish to learn PHP as well.
PHP is a server-side scripting language executed on a web server, sending HTML pages to the browser. Included in the following are instructions for the installation of Apache Web Server, PHP, MySQL database, and phpMyAdmin script used for MySQL database administration. And please note that all of the above-mentioned software can be readily obtained and used free of charge.
Programming in any computer language or script is not as impossible as one might think.
Perhaps you’re thinking that being very good at math is a prerequisite to becoming a computer programmer, however, this is not necessarily so. The average person can master computer programming. In fact, a study revealed that, among immigrants, former musicians tended to make good programmers, probably because writing music is much like writing code.
In a sense, programming is like packaging something because programming language deals with variables that are used to hold different pieces of data. What is a variable? Think of a variable as you would a box for packaging. When you select a box, you must consider the size and nature of the item (s) being packaged.
Numbers, text strings, and objects are what comprise a computer program. A variable might contain whole numbers, fractions, or pieces or strings of text. A variable that contains a whole number is called an integer. A variable holding a number with a decimal point is called a float or double, while a variable holding a piece of text is called a string.
Computer programs begin with a variable declaration and, with some exceptions, the data type. For example, in C++ or Java, you must declare the variable name and exact data type. A variable name, in any computer language, must be only one word. It may contain alphabetical characters, numbers, and underscores. Often programmers use prefixes to show variable data types. For example, strName for a string data type or int_Account for an integer data type, and so on.
The integer, double, string, and date are all data types. Each data type requires a different “space” in the computer memory. For example, an integer occupies four bytes, or, because there are 8 bits to a byte, 32 bits – a bit being the smallest unit of information. A bit may contain a 0 or a 1. In the binary system 00000001 equals 1, 00000010 equals 2, 11111111 equals 256, or 2 to the 8th power.
Four bytes can hold numbers from -2,147,483,648 to +2,147,483,647.
A Double occupies 8 bytes. Huge numbers with decimals can be held in a double. A float occupies 4 bytes and can hold a number with decimals. When selecting a gift box for a diamond ring, you wouldn’t choose a yard-by-yard box because it wouldn’t be a very efficient way to package a ring. Likewise, with variables, you’ll want to declare the data types according to the space requirements for efficient use of computer memory.
When it is important that code lines are not broken in the script, but the page width does not allow for their display without breaking, I use <=> to indicate that this line of code is to be continued, uninterrupted, on one line uninterrupted.
Declaration of variables
A computer program starts with the declaration of variables.
In PHP you do not have to declare a data type. A variable name in PHP is preceded with a $ sign. For example, $n=0; The string of text should be enclosed in either single or double quotation marks. For example:
$firstname=“Alexander’;
Or
$lastname=“Makedonski”;
$name=“Alexander Makedonski”;
As you may have noticed, a line of code in PHP is ended with a semicolon.
It’s always good practice to add comments when writing code, as they will enable you to better understand the meaning later. Also, if someone else reads or modifies your code, comments may be helpful. Comments must be preceded by two back slashes or placed between asterisks and backslashes:
/* this is code comment */
//this is another code comment
All PHP code blocks start with a "<?php” tag and ends with”? >" tag.
Basic Operators
Any computer language uses operators to calculate or compare variables, most of which are self-explanatory. For example, PHP uses + (plus), – (minus), * (multiplication), / (division), % (modulo) and "=" the assignment operator.
Let’s discuss the modulo operator. The modulo operator is used in finding the resultant remainder after one number has been divided into another. The following are a couple of examples to help you better understand:
The first example is 15% 5 = 0. As you can see, 5 divides evenly into 15 with no remainder, so the result we get is 0. Now let’s use 10% 7 = 3 as our second example. Because 7 divides into 10 once and leaves a remainder of 3, our answer must be 3.
30% 6 = 0 (30 – 5 * 6 = 30 30 – 30 = 0)
30% 7 = 2 (30 – 4 * 7 = 30 – 28 = 2)
30% 8 = 6 (30 – 3 * 8 = 30 – 24 = 6)
Comparison operators
$a == $b Equal TRUE if $a is equal to $b after type adjustment.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
$a!= $b Not equal TRUE if $a is not equal to $b after type adjustment.
$a <> $b Not equal TRUE if $a is not equal to $b after type adjustment.
$a!== $b Not identical TRUE if $a is not equal to $b, or they are not of the same type.
$a <$b Less than TRUE if $a is strictly less than $b.
$a> $b Greater than TRUE if $a is strictly greater than $b.
$a <= $b Less than or equal to TRUE if $a is less than or equal to $b.
$a> = $b Greater than or equal to TRUE if $a is greater than or equal to $b.
Let’s see an example with = = = identical operator.
$a = “10”;
$b=10;
if ($a ==$b)
echo “a = b <br>”;
else
echo “a <> b <br>”;
If ($a === $b)
echo “a and b are identical <br>”;
else
echo “a and b are not identical <br>”;
Output:
a = b
a and b are not identical
$a and $b are equal because they equal 10, but they are not identical because $a is a string and $b is an integer.
$a =