Getting Started with PHP: Day sixth of PHP

Source: Internet
Author: User
Tags constant expression getting started with php hash ini key variables

A flexible string.
Because of the flexibility of the expression, we can use the "mapping" method to bring up a string or function.
<?php
$a = ' B ';
$b = ' 123 ';
echo $ $a; This can output 123. But the better habit is the echo ${$a}, which is clearer.
$c = ' d ';
echo $c ();
Function d () {return ' 321 ';}//$c () called D functions
?>
PHP's strings are similar to JavaScript and Python, in single quotes without the use of double quotes \, and in double quotes without escaping single quotes. However, because there are interesting $ $ in PHP, double quotes are a bit more complicated. See examples.
<?php
$AA = ' abc ';
echo "' $aa '";
echo "\{$aa}"; It will show different results before writing PHP5.1.1.
echo "\\{{$aa}}"; It's okay to write that.
?>
Double quotes can have variables, you can use the "$a" or "{$a}" form to express, personal advice to use the following wording, the first editor will be highlighted, the second can write more complex formula. Be aware that although the result of "' {$a} ' and '". $a. "'" , but {is not a shorthand for ' ", Echo ' {__file__} ' and Echo '" '. __file__. "" " The results are different. Because the enclosing interval in double quotes is the structure (or ${XXXX} structure of {$ xxxx}, but this is not the same as the structure outside the double quotes, I don't like it very much). So you can write {$this->func ()}, cannot write {self::func ()}, or {constant}.
<?php
Function Demo () {return ' abc ';}
Function abc () {return ' 123 ';}
$func = function () {return ' function ';}; Need to php5.3 above version
$ABC = ' abc ';
$ABC 1 = ' Abc--one:: ';
Echo ${demo (). ' 1 '};
echo ${demo ('}} ')} ();
echo "\{{${demo ()} ()}:::{$func ()}\}{\$";
$test = ' Test ';
echo "::: <br/>{$test:: Func ()}::";
Class Test
{
static public Function func ()
{
Return ' static func ';
}
}
?>
Although PHP's double quotes are very strong, but many occasions do not need to generate the page, many times you can write this:
$form. = ' <input type= ' hidden ' name= '. $name. ' value= '. Form_prep ($value, $name). '/> '. ' \ n ";
This is a line of code for form_helper.php Form_hidden in the CI framework.
For a long list of single quotes to output, enclose the string in double quotes. Instead, use the unit quotes. Single quotes and double quotes are less efficient at enclosing strings, although single quotes only need to escape \ and \, and double quotes have
Sequence Meaning
\ n linefeed (LF or 0x0A (in ASCII)
\ r Carriage return (CR or 0x0D () in ASCII)
\ t Horizontal tab (HT or 0x09 (9) in ASCII)
\v Vertical tab (VT or 0x0b in ASCII) (since PHP 5.2.5)
\f Page change (FF or 0x0C () in ASCII) (since PHP 5.2.5)
\\ Back slash
\$ Dollar Dollar mark
\" Double quotes
\[0-7]{1,3} A string that conforms to the order of the expression is a octal character
\x[0-9a-fa-f]{1,2} A string that matches the order of the expression is a hexadecimal character
So much to escape, but I think that when the program is traversing every character, it actually initially needs to be judged \ "$, and possibly {.
So how should double quotes and single quotes be exported? Let's take a look at the solutions for other languages first.
Python can use ' or ' ' to close a string without escaping ' and ', if necessary, writing ' such ', ' and ' '.
Because PHP is a template language, so want to directly output the words (it is best to write large references in the template file), directly to write HTML.
Without direct output, PHP uses the HEREDOC syntax structure and nowdoc (syntax structure from PHP 5.3.0).
Heredoc is the beginning of the <<<xxxx, with XXXX; The end, Nowdoc, is the beginning of <<< ' xxxx ' to XXXX; Note xxxx; You must not have another character between the carriage return and the return statement.
<?php
$a = ' abc ';
$b = ' html ';
$c = <<' {$b} '
html
$d = "' {$b} '
";
if ($c = = = $d)
{
Echo &LT;&LT;&LT;ABC
\ $c = = = $d::: ' {$a} ' {$c}
Abc<br/>
abc
echo <<< ' Test '
\ $c = = = $d::: ' {$a} ' {$c}
' Test '
Test<br/>
Test
}
?>
JavaScript has no native method, but relies on HTML to solve the problem.
Here is an example of an IFRAME asynchronous submission.
<?php
* iframe_test.php * *
if (isset ($_post[' cmd '))
{
Formal writing is the reference template file.
?>
<textarea id= "Show-div" ><?php
echo $_post[' textarea '];
?></textarea>
<script>
var a = document.getElementById ("Show-div"). Value;
Parent.show (document.getElementById ("Show-div"). Value);
</script>
<?php
}
?>
<! DOCTYPE html>
<meta charset= "Utf-8" >
<title>welcome to Codeigniter</title>
<body>
<iframe name= "iframe_show" width= "0" height= "0" scrolling= "no" style= "Display:none" >
</iframe>
<form name= "mainform" target= "Iframe_show" method= "post" enctype= "Multipart/form-data" >
<textarea name= "textarea" ><script>alert (123) </script></textarea>
<input type= "Submit" name= "cmd" value= "submitted"/>
<input type= "Reset" value= "undo"/>
</form>
<div id= "Show" >hello world</div>
</body>
<style type= "Text/css" >
</style>
<script>
<! [cdata[
function Show (str) {
alert (str);
document.getElementById ("Show"). InnerHTML = str;
}
]]>
</script>
String as the key of the array:
PHP's array has only two types of keys, integers and strings, and ' 0 ' ~ ' 9 ' will be recognized as integers or turned into integers. See the following example.
<?php
$arr _type = Array (
0x34 => ' 0x34 ',
1 => ' 1 ',
' 1 ' => ' 1 ',
1.99 => ' 1.99 ',
True => ' true ',
0 => ' 0 ',
False => ' false ',
' 0 ' => ' 0 ',
0.0 => ' 0.0 ',
0.01 => ' 0.01 ',
. => ' 0.99 ',
-1 => '-1 ',
' 1.99 ' => ' ' 1.99 '
'",
);
Var_dump ($arr _type, Json_encode ($arr _type), Json_encode ($arr _type));
if (1!== true)
{
echo Count ($arr _type), ' <br/> ';
Echo $arr _type[1.99], ' <br/> ';
Echo $arr _type[false], ' <br/> ';
echo $arr _type[' 1.99 '], ' <br/> ';
Echo $arr _type[-1], ' <br/> ';
Echo $arr _type[0x34], ' <br/> ';
}
?>
The array in the example above is only five elements, which is somewhat predictable, but the following echo can be output normally with a non-integer key such as false 1.99. Programming is a strict matter, we do not want to have many unexpected results, and this escape to some extent affect efficiency. This data structure in other languages will have strict patterns and loose patterns, such as Lisp, which has only strict patterns (such as hash={' 1 ': 1}; print (hash[1)) #这个会报错--saying Python is a scripting language, But the first design is much stricter than other scripting languages, so testability is better than other scripting languages. If you want to design a language of your own, first to the strict direction of design, leaving an INI for others to configure, the written procedures, from strict to loose easy, relaxed to strict mode, many times completely impossible or visually impossible. But the PHP array, only this kind of loose mode, hope php5.6 can come up with something new.
JavaScript is pretty much the same, but the keys are strings. Code written if the key is negative, does not turn into a string. Ture and Flase will be converted to ' true ' and ' flase '.
<script>
var arr = {
A: ' 47a ',
' B ': 12,
1: ' 1 ',
/*-1: '-1 ', */
1.99: ' 1.99 ',
'-1 ': '-1 ',
False: ' false ',
0: ' 0 ',
' 0 ': ' char0 ',
};
var i;
var str = ';
For (i in arr) {
STR + + typeof (i) + ': ' +i+ ' => ' +arr[i]+ ' \ n ';
}
if (1!== true) {
Alert ([
arr[' 1 '],
ARR[1.99],
Arr[-1],
Arr[' A '],
Arr[false],
Arr[0],
arr[' 0 '],
Str
]);
}
</script>
Dart is similar to JavaScript and can only be used as a string, but not escaped. The following example will make an error. The individual is not optimistic about the future of dart, but when it comes to writing something, take a look at it. Sure enough, the language is different, there are a variety of micro-differences, think of their own many times PHP use more, use JS when it is assumed to be so, it is funny. The following DART code will complain
void Main () {
Print ("Hello, world!");
var a = {' 1 ': 1};
var B = {1:1}; The reason for the error
Print (a[' 1 ']);
Print (a[1]); The reason for the error
}
In some configurations, PHP can write this, $arr = Array (' key ' => ' Val '); echo $arr [key]; But this type of writing, PHP will first see whether the key is constant (PHP has thousands of constants), if not a constant, as a string. So this writing efficiency is very low, than $arr[' key '] several times slower (constant many items, but also more slowly). echo "$arr [key]" is almost as efficient as the echo {$arr [' key ']}, because constants are not considered in double quotes. Generally speaking $arr[key] this kind of writing less typing and clear, but more dozen two "will make your program environmental protection Some, common some, it is not difficult to do."
If PHP constants are like variables, there is a prefix in front, such as #, which has a//instead of #. General write $arr[key] do not have to consider the constant, high efficiency and convenient for programmers. Writing $arr[#key] is not very much and is written $arr[' #key ' when needed. or learn about Erlang (I don't like Erlang's inherited grammar from the Prolog, more accustomed to looking at the syntax of C or LISP, so that "variable" can only be named with uppercase and underline, not plain caps and underscores for the name of "Atom"; PHP's own constants are all uppercase, I've seen PHP projects where constants are named with caps and underscores, and probably rarely, and the name value of a form is lowercase in most items, so that most arrays are just plain caps and underscores. If 5.6 can out this function, change the INI on the implementation, most of the company's PHP new projects, estimates will use this function.
This article links http://www.cxybl.com/html/wlbc/Php/20130729/39380.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.