+2347067162698
tclassified.com@gmail.com

PHP STACK UP

Educational Technology and ICT

Created with Sketch.

PHP STACK UP

I. Some Basics

A. PHP is a scripting language – it gets interpreted instead of being compiled like C++ and Java.

B. Unlike JavaScript which is executed by the web browser, all PHP code is executed on the web server.

C. The syntax is very similar to Perl and C. Variables are case sensitive, function names are not, and statements must be terminated with a semicolon.

D. PHP code should be placed between or tags. The second method is preferred so your scripts are XML compatible. There is no limitation as to where PHP code can be inserted.

E. To see information about how PHP is configured, version information, and the settings of all environment variables (e.g., HTTP_USER_AGENT and QUERY_STRING), call the phpinfo() function in any script.

F. The php.ini file is the main configuration file for PHP. It can be edited by the system administrator to change any of the configuration settings. A change to this file requires the web server be restarted since the file is only read once when the web server starts up. (The phpinfo() function reports the location of php.ini on the server.) 3

G. It’s a good idea to turn on error and warning output when developing your code so you don’t misuse PHP syntax in unintended ways. Place the following lines of code at the top of your script so errors will be reported in the rendered web page: ini_set(‘display_errors’, ‘1’); error_reporting(E_ALL | E_STRICT); Note that if the php.ini file already has these settings, you don’t need to use these lines of code.

To code PHP you need

Apache/MySql Server(XAMPP, WAMP, ARPSERVER etc)

Editor( notepad or vscode)

Browser( Chrome, Edge, Safari, Firefox)

II. Comments

The three following styles are legal: # Perl style single line comment // Single line comment /* Multiple line comments */

III. Variables and Data Types

A. Always starts with $ and letter or underscore. Can be composed of numbers, underscores, and letters. $my_var = 10; $a_2nd_var = “bison”;

B. Data types: integers, doubles (numbers with a decimal point), boolean (true or false), NULL, strings, arrays, objects, and resources (like database connections). Variables do not have to be declared and neither do their data types.

C. Variables have a default value (0, empty string, false, or empty array) if they aren’t initialized before trying to use them. It’s always good practice to initialize all variables rather than relying on the default initialization value. If you try to use a variable before setting it to a value, strict error-reporting setting will give you an “Undefined variable” warning.

D. All variables have local scope (i.e., they are accessible only within the function or block in which they are initialized). Global variables may only be accessed within a function by using the global keyword. $x = “test”; function display() { global $x; echo $x; }

IV. Operators

A. Assignment 1. = += -= /= *= %= ++ — – like most programming languages. 2. .= – string concatenation operator (see strings section).

B. Arithmetic 1. + – * / % – like most programming languages.

C. Comparison 1. == != < > <= >= – like most programming languages. Also <> is the same as !=. 2. === – true if arguments are equal and the same data type. 3. !== – true if arguments are not equal or they are not of the same data type.

D. Logical 1. && || ! – like most programming languages (&& and || short-circuit) 2. and or – like && and || but have lower precedence than && and ||. 3. xor – true if either (but not both) of its arguments are true.

V. Input/Output

A. print and echo are used to print to the browser. echo “Go Bisons”; echo(“Go Bisons”); // same thing print(“Go Bisons”); // same thing

B. print can only accept one argument, and echo can accept any number of arguments. print returns a value that indicates if the print statement succeeded.

C. Variables are interpolated inside of strings unless single quotes are used. $a = “guts”; echo “You have $a.”; // prints “You have guts.” echo ‘You have $a.’; // prints “You have $a.”

D. Escape sequences: \n (newline), \r (carriage-return), \t (tab), \$ ($), \” (“), \\ (\) echo “a\\b\tc\$d”; // prints “a\b c$d” echo ‘a\\b\tc\$d’; // prints “a\b\tc\$d”. Only \\ is converted.

E. printf works like C’s counter-part. $title = “X-Men”; $amount = 54.235; printf(“The movie %s made %2.2f million.”, $title, $amount); // prints “The movie X-Men made 54.23 million.”

F. PHP typically does not run from the command-line, but input from the keyboard can be accessed using the fopen function with “php://stdin”. See the file I/O section for more information.

G. Output shortcut from within HTML: Hello, is the same as Hello,?> 5

VI. Control Structures

A. Choice structures 1. if ($x > 0) $y = 5; // {} not required for only one statement

2. if ($a) { // tests if $a is true or non-zero or a non-empty string print($b); $b++; } else print($c);

3. if ($a > $b) print “a is bigger than b”; elseif ($a == $b) // use “elseif” or “else if” print “a is equal to b”; else print “a is smaller than b”;

4. switch ($vehicle_type) { // works for integers, floats, or strings case “car”: $car++; break; case “truck”: $truck++; break; case “suv”: $suv++; break; default: $other++; }

B. Looping structures

1. while ($n < 10) { print(“$n “); $n++; }

2. do { print(“$n “); $n++; } while ($n < 10);

3. for ($n = 1; $n < 10; $n++) print(“$n “);

4. foreach ($myarray as $item) print(“$item “);

For More on PHP, Contact us via;+2347067162698

Leave a Reply

Your email address will not be published. Required fields are marked *

Open chat