Showing posts with label PHP Tutorial. Show all posts
Showing posts with label PHP Tutorial. Show all posts

PHP For Beginners - Lesson 6


1. You can compare two values for equality with the == comparison operator (for example, if ($a == 23) …).
2. To test whether two values are the same and of the same type, you use the === identity operator (for example, if ($b === '23') …).
3. The results of the expressions are a) FALSE, b) TRUE, c) TRUE, and d) FALSE.
4. The result of !(23 === '23') is TRUE, because 23 is not identical to '23' (they have the same numeric value, but one is a number and one is a string), so the inner part evaluates to FALSE, and the ! (not) operator negates this, turning the final value into TRUE.
5. To set the variable $bulb to the value 1 when the variable $daypart has the value 'night', and 0 when it doesn’t, you can use the ternary expression: $bulb = ($daypart == 'night') ? 1 : 0;.
6. PHP will evaluate the expression 5 * 4 + 3 / 2 + 1 using precedence, with * and / having greater precedence than +. The result will be 5 * 4 (which is 20) plus 3 / 2 (which is 1.5) plus 1, making 22.5, the same as (5 * 4) + (3 / 2) + 1.
7. To force PHP to evaluate the expression 1 + 2 / 3 * 4 – 5 from left to right, you need to place parentheses in appropriate places. There are different ways to do this, including (1 + 2) / 3 * (4 – 5), which increases the precedence of the + and – operators to that of / and *, but requires a human to determine where to place the parentheses. Alternatively, you can systematically parenthesize each pair of operands and their operator in turn, like this (with the final pair not requiring any parentheses): (((1 + 2) / 3) * 4) – 5.
8. The math operators have left-to-right associativity because the value on the left is being applied to the value on the right, by the operator.
9. The assignment operators have right-to-left associativity because the value on the right is being assigned to the item on the left.
10. It is a good idea to place the most likely to be TRUE expression on the left of the || operator because it has left-to-right associativity. This means that the left-hand expression is tested first, and if it evaluates to TRUE, the right-hand expression will not be tested (thus saving processor cycles). The right-hand expression will only be tested if the left-hand one evaluates to FALSE.

PHP For Beginners - Lesson 6

PHP For Beginners - Lesson 5


1. The four main mathematical operators (and their symbols in PHP) are plus (+), minus (-), multiply (*), and divide (/).
2. To increment or decrement a variable, you use the increment (++) or decrement (--) operator, respectively.
3. The difference between pre- and post-incrementing and pre- and post-decrementing is the position in which you place the operator. To pre-increment a variable, you place the increment operator in front of it (for example, ++$a). To post-increment, you place it afterwards (for example, $a++). Pre- and post-decrementing are similar (for example, --$a and $a--). Pre-incremented and pre-decremented variables have their value changed before it is accessed for use in an expression, whereas post-incremented and post-decremented variables first have their value used in an expression, and only after that is it changed.
4. The modulus operator symbol is %, and it returns the integer remainder after a division.
5. To return a number as a non-negative value, regardless of whether it is positive or negative, pass it through the abs() function (for example, $absvalue = abs($myvalue);).
6. In this question, the task is to not allow $v to be negative; therefore, you should use the max() function (even though at first sight it appears that min() should be the answer, because we want a minimum value), like this: $v = max(0, $v);. If $v is less than 0, then the 0 argument will pull it up to 0, because 0 is the maximum of the two values. But if $v is greater than 0, then the argument of $v itself will be the maximum value, and so the same positive value will be assigned back to the variable.
7. To obtain a pseudo-random number between 1 and 100, inclusive, you call the rand() function, passing the minimum and maximum values (for example, $randnum = rand(1, 100);).
8. Using the assignment += operator, you can shorten expressions such as $a = $a + 23; to $a += 23;.
9. If $a has the value 58, the expression $a /= 2; will evaluate to 29.
10. To set the variable $n to contain the remainder after dividing it by 11, you can use the expression $n %= 11;.

PHP For Beginners - Lesson 5

PHP For Beginners - Lesson 4


1. A constant is similar to a variable in that it stores a value that becomes accessible by name (but without the preceding $ character that is used to access variables). However, unlike a variable, once a value is assigned to a constant, it can never be changed.
2. You define constants using the define() function, passing it a name and a value (for example, define('MAX_USERS', 128);).
3. Predefined constants are those that PHP has already defined for you to provide information you can access such as the path and filename of the current file (for example, echo __FILE__;).
4. The print and echo commands are very similar to each other in that they both display values provided to them. The differences are that print accepts only a single argument but has a return value of 1, so it can be used in some places where echo cannot, but echo is a little faster and also supports multiple arguments separated by commas (which print does not).
5. The statement ($var == TRUE) ? echo "true" : echo "false"; is not valid PHP because echo doesn’t return a value and therefore cannot be used within expressions. In such instances, print can be used instead (for example, ($var == TRUE) ? print "true" : print "false";).
6. The superglobal arrays that handle information sent to a PHP program via forms sent using Get and Post methods are $_GET[] and $_POST[].
7. The $_COOKIE[] superglobal array contains cookie data.
8. You can display the URL of a page from which a user was referred to the current one (if available) with a statement such as echo $_SERVER[ 'HTTP_REFERER'];.
9. To sanitize input and other data by replacing any HTML tags with entities that only display the tag names, you can run them through the htmlentities() function (for example, echo htmlentities($_POST['bio']);).
10. You can get PHP to display its configuration information and the current environment and script with the phpinfo() function—for example, phpinfo(32); will display the PHP predefined variables.

PHP For Beginners - Lesson 4

PHP For Beginners - Lesson 3


1. PHP is case-sensitive. This means that the combination of uppercase and lowercase characters used in variables, objects, and function names is important (for example, $todaysdate is a different variable to $TodaysDate).
2. Any combination of spaces, tabs, linefeeds, and some other non-alphanumeric or punctuation characters is known as whitespace.
3. PHP generally ignores whitespace characters that appear outside of strings—whitespace within strings is stored and acted upon, though.
4. A numeric variable is a container for a number, which allows you to address the value using a name (for example, $age = 32;).
5. A string variable is a container for a sequence of characters, which can be addressed using a name (for example: $name = 'Albert Einstein';).
6. You can include quotation marks within a string (regardless of whether or not the same type is used to contain the string) by escaping them with the \ escape character, which can also be used to insert special characters (for example, $mystring = "He said, \"Hello\"\n" ;—which also includes a linefeed at the end).
7. A heredoc string is one that is not enclosed in any type of quotation mark. Instead, a token is used to denote the string’s start and end, and it is commonly used in conjunction with an echo statement or string assignment. The tokens are generally given a preceding underline character to help them stand out (although not required), and the closing token must appear at the very start of a new line (with no indentation and no spaces or tabs before or after the semicolon).
8. PHP variables do not necessarily retain the type they are initially assigned, because if an operation is performed on them that makes better sense if the content were of a different type, then PHP will change the type. Therefore, a string containing all digits is turned into a number by PHP if a mathematical operation is performed on it—for example, the following will output the number 1235, even though $n is initially a string: $n = '1234'; $n = $n + 1; echo $n;.
9. To force PHP to store a certain type of value in a variable, you can use a casting keyword such as (int) or (string). An example would be $n = (int) '1234'; or $s = (string) (12 * 34);.
10. In PHP, to display the values of variables within a string, you do not need to break the string into smaller parts and then use the concatenation operator to splice the parts and variables together (as you must in some languages). Instead, just enclose the string in double quotation marks (not single quotes) and then drop the variable names right in where they are needed (for example, echo "Hello $name, you have previously visited on $times occasions.";).

PHP For Beginners - Lesson 3

PHP For Beginners - Lesson 2


1. You can place sections of PHP code anywhere in a document, but generally will do so in either the body or head (or you can make an entire document PHP, which then uses commands to output HTML).
2. To include a file of PHP instructions into a document, you can use an include, require, include_once, or require_once statement (for example: include 'header.php';).
3. To prevent an external PHP file from being included multiple times, you can use the include_once or require_once statement (for example, include_ once 'header.php';). Any additional attempts to load the file will be ignored.
4. To ensure that a PHP file gets included in a document, you can use the require statement (for example: require 'header.php';). If the file cannot be loaded, an error will be thrown.
5. To ensure that a PHP file is included and that it doesn’t get included more than once, you can use the require_once statement (for example: require_once 'heading.php';). If the file cannot be loaded, an error will be thrown, and any additional attempts to load the file will be ignored.
6. To create a single-line comment in PHP, start it with the // comment marker (for example, // This is a comment). All text after the comment marker to the end of the line is ignored.
7. To create a multiline comment in PHP, you start it with /* and end it with */ (for example, /* The contents between and including these two comment markers is completely ignored by PHP */).
8. To indicate that a PHP instruction is complete, you must place a semicolon character (;) after it (for example, echo "Hello world";). Failure to do this is one of the most common causes of errors for beginners.
9. The code $items = 120; $selection = 7; is legal PHP because multiple statements are allowed on a line, due to requiring semicolons after each instruction.
10. The code $items = 120 $selection = 7; will cause PHP to throw a parse error because there is no semicolon after the number 120 to separate the statements.

PHP For Beginners - Lesson 2

PHP For Beginners - Lesson 1


1. PHP is available for Windows, Mac OS X, and Linux/Unix computers.
2. PHP is compiled only at runtime, effectively making it a scripted language.
3. You can include as many sections of PHP as you like in an HTML document.
4. You must place a $ character in front of all PHP variables.
5. PHP supports both procedural and object-oriented programming (OOP).
6. You should give PHP documents the file extension .php (although servers can be configured to use PHP with any file extension).
7. The five main browsers with which you should test your PHP programs are Microsoft Internet Explorer, Apple Safari, Google Chrome, Mozilla Firefox, and Opera. You should also test your programs on tablets and mobile devices, too, if your target market uses them.
8. You can write and edit PHP programs using a plain text editor, but for better control and handy development tools you may prefer to use a program editor, or an integrated development environment (IDE).
9. There are several PHP server suites (also known as stacks) on the Internet. I recommend XAMPP, which is easy to download, install, and use right away.
10. After installing a web server such as Apache (included with XAMPP), your PHP files should be placed in its document root folder, whose location will vary according to operating system.

PHP For Beginners - Lesson 1