Introduction to PHP What is PHP? PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source generalpurpose scripting language that is especially suited for web development and can be embedded into HTML. The PHP code is enclosed in special start and end processing instructions <?php and ?> that allow you to jump into and out of "PHP mode." What distinguishes PHP from something like client-side JavaScript is that the code is executed on the server, generating HTML which is then sent to the client. The client would receive the results of running that script PHP tags When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. PHP also allows for short tags <? and ?> (which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two. Short tags (example three) are only available when they are enabled via the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. ASP style tags (example four) are only available when they are enabled via the asp_tags php.ini configuration file directive. Comments: PHP supports 'C', 'C++' and Unix shell-style (Perl style) comments. For example: Types : PHP supports eight primitive types. Four scalar types: • • • • boolean integer float (floating-point number, aka double) string Two compound types: • array • object And finally three special types: • resource • NULL • callable This manual also introduces some pseudo-types for readability reasons: • mixed • number • callback Booleans This is the simplest type. A boolean expresses a truth value. It can be either TRUE or FALSE. Integers: Floating point numbers Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: <?php $a = 1.234; $b = 1.2e3; $c = 7E-10; ?> Strings: A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support. Note: string can be as large as 2GB. A string literal can be specified in four different ways: • • • • single quoted double quoted heredoc syntax nowdoc syntax (since PHP 5.3.0) Single quoted: <?php echo 'this is a simple string'; echo 'You can also have embedded newlines in strings this way as it is okay to do'; // Outputs: Arnold once said: "I'll be back" echo 'Arnold once said: "I\'ll be back"'; ?> Double quoted <?php echo “this is a simple string”; ?> <?php $var='tester'; $name="$var"; echo $name; ?> O/P : tester. <?php $var='tester'; $name='$var'; echo $name; ?> O/P : $var. Heredoc A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore. <?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; echo $str; ?> Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Nowdoc Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. Nowdoc support was added in PHP 5.3.0. <?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; echo $str."<br>"; $name="Naveen"; $str = <<<'EOD' Example of $name EOD; echo $str."<br>"; $name="Naveen"; $str = <<<EOD Example of $name EOD; echo $str; ?> Arrays An array in PHP is actually an ordered map. A map is a type that associates values to keys. PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer. An example of PHP's automatic type conversion is the addition operator '+'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is. Type Casting Type casting in PHP works much as it does in C: the name of the desired type is written in parentheses before the variable which is to be cast. Basics Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. Variable variables Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as: <?php $a = 'hello'; ?> A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e. <?php $$a = 'world'; ?> At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement: <?php echo "$a ${$a}"; ?> produces the exact same output as: <?php echo "$a $hello"; ?> i.e. they both produce: hello world. Variables From External Sources HTML Forms (GET and POST) When a form is submitted to a PHP script, the information from that form is automatically made available to the script. There are many ways to access this information, for example: Constants A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants, which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. <?php define("FOO","something"); ?>