PHP Part FAQ Summary One

Source: Internet
Author: User
Tags explode parse error php code

PHP Part FAQ Summary-REPRINT

1: Why I can't get a variable

I post data name to another page on one page, why can't I get any value when I output $name?

Register_global defaults to off in later versions of PHP4.2
To obtain a variable submitted from another page:

Method One: Find the Register_global in the php.ini and set it to on.
Method Two: The Extract ($_post) is placed at the front of the receiving page, and extract ($_get) must have extract () before the $_session (Session_Start) is noted.
Method Three: One reads the variable $a=$_get["a"]; $b =$_post["B"] and so on, this method although troublesome, but relatively safe.

2: Debug Your Program

You must know the value of a variable at run time. I did that by creating a document debug.php, which reads as follows:

PHP Code:--------------------------------------------------------------------------------

? Php
Ob_start ();
Session_Start ();
Echo "<pre>";

Echo "The _get variables obtained on this page are:";
Print_r ($_get);

Echo "The _post variables obtained on this page are:";
Print_r ($_post);

Echo "The _cookie variables obtained on this page are:";
Print_r ($_cookie);

Echo "The _session variables obtained on this page are:";
Print_r ($_session);
Echo "</pre>";
?>

--------------------------------------------------------------------------------

Then set it in php.ini: include_path = "c:/php" and place debug.php in this folder,
You can then include this file in each page to see the resulting variable name and value.

3: How to use Session

When it comes to session, you must call the function session_start ();

Paying for the session is simple, such as:


PHP Code:--------------------------------------------------------------------------------

<?php
Session_Start ();
$Name = "This is a session example";
Session_register ("Name");/note, do not write: Session_register ("$Name");
Echo $_session["Name"];
Then $_session["Name" as "This is a session example"
?>

--------------------------------------------------------------------------------



After php4.2, you can pay the value for the session directly:

PHP Code:--------------------------------------------------------------------------------

? Php
Session_Start ();
$_session["Name"]= "value";
?>

--------------------------------------------------------------------------------

Canceling a session can be like this:

PHP Code:--------------------------------------------------------------------------------

<?php
Session_Start ();
Session_unset ();
Session_destroy ();
?>

--------------------------------------------------------------------------------


Cancel a session variable above php4.2 and bug.



Attention:

1: cannot have any output before calling Session_Start (). For example, the following is wrong.
==========================================
1 lines
2 line;? Php
3 line Session_Start ();//previous output in the first line
4 lines ...
5 Lines?>
==========================================


Tip 1:

The "... headers already sent ..." is the output of the information to the browser before the Session_Start () is present.
Remove output is normal, (cookies can also appear this error, the same reason for error)

Tip 2:

If your session_start () is in a looping statement and it is difficult to determine where to output the information to the browser before, you can use the following method:
1 line;? PHP Ob_start ();?>
........ Here is your program ...



2: What's wrong with that?

Warning:session_start (): Open (/tmpsess_7d190aa36b4c5ec13a5c1649cc2da23f, O_RDWR) failed: .....
Because you did not specify a path for the session file to be stored.

Workaround:
(1) Create folder TMP in C disk
(2) Open php.ini, find Session.save_path, modify to Session.save_path= "C:/tmp"



4: Why I transfer a variable to another page, only the first half of the part, the beginning of a space is all lost


PHP Code:--------------------------------------------------------------------------------

<?php
$Var = "Hello php";//Modify to $var= "Hello php"; try to get a result
$post = "receive.php?" Name= ". $Var;
Header ("Location: $post");
?>

--------------------------------------------------------------------------------

Content of receive.php:

PHP Code:--------------------------------------------------------------------------------

? Php
Echo "<pre>";
Echo $_get["Name"];
Echo "</pre>";
?>

--------------------------------------------------------------------------------


The correct approach is to:

PHP Code:--------------------------------------------------------------------------------

<?php
$Var = "Hello php";
$post = "receive.php?" Name= ". UrlEncode ($Var);
Header ("Location: $post");
?>

--------------------------------------------------------------------------------


On the receiving page you do not need to use UrlDecode (), the variable is automatically encoded.


5: How to intercept the specified length of Chinese characters without appearing with the "?>" End, Beyond the section "..." instead


In general, the variables to be intercepted are from MySQL, first of all to ensure that the field length is long enough, generally char (200), you can maintain 100 characters, including punctuation.

PHP Code:--------------------------------------------------------------------------------

? Php
$str = "This character is so long, ^_^";
$Short _str=showshort ($STR, 4);//intercept the front 4 characters, the result is: this character ...
Echo "$Short _str";
Function csubstr ($str, $start, $len)
{
$strlen =strlen ($STR);
$clen = 0;
for ($i =0; $i < $strlen; $i + +, $clen + +)
{
if ($clen >= $start + $len)
Break
if (Ord (substr ($str, $i, 1)) >0xa0)
{
if ($clen >= $start)
$tmpstr. =substr ($str, $i, 2);
$i + +;
}
Else
{
if ($clen >= $start)
$tmpstr. =substr ($str, $i, 1);
}
}

return $tmpstr;
}
Function Showshort ($STR, $len)
{
$tempstr = csubstr ($str, 0, $len);
if ($str <> $tempstr)
$tempstr. = "..."; What you want to end with, modify it here.

return $tempstr;
}

--------------------------------------------------------------------------------



6: Specification of your SQL statement


In the table, precede the field with "'" so that no error occurs because of misuse of the keyword.
Of course I don't recommend using keywords.

For example
$SQL = "INSERT into ' xltxlm ' (' Author ', ' title ', ' id ', ' content ', ' Date ') VALUES (' xltxlm ', ' use ', 1, ' criterion your Sql s Tring ', ' 2003-07-11 00:00:00 ')

"'" How to enter? Above the TAB key.


7: How to make the string in html/php format not interpreted, but as shown


PHP Code:--------------------------------------------------------------------------------

? Php
$str = "Echo "was explained:". $str. " <br> treated by: ";
Echo htmlentities (NL2BR ($STR));
?>

--------------------------------------------------------------------------------



8: How to get the value of the variable outside the function in the function


PHP Code:--------------------------------------------------------------------------------

? Php
$a = "PHP";
Foo ();
Function foo ()
{
Global $a;//delete here to see what's the result
Echo "$a";
}
?>

--------------------------------------------------------------------------------



9: How do I know what function the system supports by default?


PHP Code:--------------------------------------------------------------------------------

<?php
$arr = Get_defined_functions ();
Function php () {
}
echo "<pre>";
Echo "Here shows all the functions supported by the system, and the custom function PHPN";
Print_r ($arr);
echo "</pre>";
?>
--------------------------------------------------------------------------------


10: How to compare two date difference days


PHP Code:--------------------------------------------------------------------------------

? Php
$Date _1= "2003-7-15";//may also be: $Date _1= "2003-6-25 23:29:14";
$Date _2= "1982-10-1";
$Date _list_1=explode ("-", $Date _1);
$Date _list_2=explode ("-", $Date _2);
$d 1=mktime (0,0,0, $Date _list_1[1], $Date _list_1[2], $Date _list_1[0]);
$d 2=mktime (0,0,0, $Date _list_2[1], $Date _list_2[2], $Date _list_2[0]);
$Days =round (($d 1-$d 2)/3600/24);
Echo "I have struggled for $Days days ^_^";
?>

--------------------------------------------------------------------------------


11: Why I upgraded PHP, the original program appears full screen of notice:undefined variable:


This is the meaning of the warning because the variable is undefined.
Open PHP.ini, find the bottom error_reporting, change to error_reporting = E_all & ~e_notice

For parse error errors
Error_reporting (0) cannot be closed.
If you want to turn off any error prompts, open php.ini, find display_errors, set to Display_errors = off. Any future errors will not be prompted.

Then what is error_reporting?

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.