Home > Backend Development > PHP Tutorial > PHP study notes (1), PHP study notes_PHP tutorial

PHP study notes (1), PHP study notes_PHP tutorial

WBOY
Release: 2016-07-13 09:45:33
Original
953 people have browsed it

php study notes (1), php study notes

1. There are two ways to reference files: require and include

The usage of require is as follows: require("MyRequireFile.php");. This function is usually placed at the front of the PHP program. Before the PHP program is executed, it will first read in the file specified by require and make it a part of the PHP program web page. Commonly used functions can also be introduced into web pages in this way.

 include is used as follows: include("MyIncludeFile.php");. This function is generally placed in the processing part of flow control. The PHP program web page only reads the include file when it reads it. In this way, the process of program execution can be simplified.

2. Comments

<?<span>php
    </span><span>echo</span> "这是第一种例子。\n"; <span>//</span><span> 本例是 C++ 语法的注释</span>
    <span>/*</span><span> 本例采用多行的
       注释方式      </span><span>*/</span>
    <span>echo</span> "这是第两种例子。\n"<span>;
    </span><span>echo</span> "这是第三种例子。\n"; <span>#</span><span> 本例使用 UNIX Shell 语法注释</span>
  ?>
Copy after login

Comment: The information explained is what and why.

3. Constant type

PHP defines the following constants in constants.

 __FILE__
 This default constant is the name of the PHP program file. If a file is referenced (include or require), the constant in the referenced file is the referenced file name, not the file name that refers to it.

 __LINE__
 This default constant is the number of PHP program lines. If a file is referenced (include or require), the constant in the referenced file is the line that refers to the file, not the file line that references it.

 PHP_VERSION
This built-in constant is the version of the PHP program, such as '3.0.8-dev'.

PHP_OS
This built-in constant refers to the name of the operating system running the PHP parser, such as 'Linux'.

TRUE
This constant is the truth value (true).

 FALSE
 This constant is a false value (false).

E_ERROR
This constant points to the most recent error.

E_WARNING
This constant points to the nearest warning.

E_PARSE
This routine is a potential problem with parsing grammar.

E_NOTICE
This routine indicates something unusual but not necessarily an error. For example, access a variable that does not exist.

 For these constants starting with E_, please refer to the error_reporting() function for more related instructions.

Of course, when writing programs, the above default constants are not enough. The define() function allows us to define the constants we need. See example below

<?<span>php
    </span><span>define</span>("COPYRIGHT", "Copyright &copy; 2000, netleader.126.com"<span>);
    </span><span>echo</span><span> COPYRIGHT;  

    </span><span>echo</span> <span>__FILE__</span><span>;  

  </span>?>
Copy after login

4. Declare variables (case sensitive)

<?<span>php
 </span><span>/*</span><span>*
 * @file variable.php
 * @author suguolong
 * @date 2015/07/29 16:49:08
 * @brief 
 *  
 *</span><span>*/</span>

<span>/*</span><span> 定义字符串变量 </span><span>*/</span>
<span>$mystring</span> = "我是字符串"<span>;
</span><span>$WilsonPeng</span> = "真是认真的作者"<span>;
</span><span>$NewLine</span> = "换行了\n"<span>;

</span><span>/*</span><span> 定义整型变量 </span><span>*/</span>
<span>$int1</span> = 38<span>;
</span><span>$int2</span> = 49<span>;
</span><span>$hexint</span> = 0x10<span>;

</span><span>/*</span><span> 定义浮点变量 </span><span>*/</span>
<span>$float1</span> = 1.732<span>;
</span><span>$float2</span> = 1.4E+2<span>;

</span><span>/*</span><span> 定义数组变量 </span><span>*/</span>
<span>$MyArray1</span> = <span>array</span>("子", "丑", "寅", "卯"<span>);
</span><span>$MyArray2</span> = <span>array</span><span>(
              </span>"地支" => <span>array</span>("子", "丑", "寅", "卯"),
              "生肖" => <span>array</span>("鼠", "牛", "虎", "兔"),
              "数字" => <span>array</span>(1, 2, 3, 4<span>)
            );

</span><span>/*</span><span> 类的定义 </span><span>*/</span>
<span>class</span><span> foo {
  </span><span>function</span><span> do_foo () { 
    </span><span>echo</span> "Doing foo.\n"<span>; 
  }
}

</span><span>/*</span><span> 类的使用 </span><span>*/</span>
<span>$bar</span> = <span>new</span><span> foo;
</span><span>$bar</span> -><span> do_foo ();
</span><span>$bar</span> -><span> do_foo ();
</span><span>$bar</span> -><span> do_foo ();

</span><span>/*</span><span> 定义布尔值 </span><span>*/</span>
<span>$booleanval_true</span> = <span>true</span><span>;
</span><span>$booleanval_false</span> = <span>false</span><span>;

</span><span>/*</span><span> 使用变量 </span><span>*/</span>
<span>echo</span> "boolean value of true: \n"<span>;
</span><span>echo</span>  <span>$booleanval_true</span><span>;
</span><span>echo</span> "\n"<span>;

</span><span>echo</span> "boolean value of false: \n"<span>;
</span><span>echo</span>  <span>$booleanval_false</span><span>;
</span><span>echo</span> "\n"<span>;


</span><span>/*</span><span> vim: set expandtab ts=4 sw=4 sts=4 tw=100: </span><span>*/</span>
?>
Copy after login
[suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$ php variable.php 
Doing foo.
Doing foo.
Doing foo.
boolean value of true: 
1
boolean value of false: 

[suguolong@cp01-rdqa-dev004.cp01.baidu.com sugl]$ 
Copy after login

5. Use of variables

When the PHP program is executed, the system will reserve an area of ​​global variables in the memory. In actual use, you can retrieve the required variables through $GLOBALS["Variable Name"].

 The $GLOBALS array is a special variable in the PHP program. It does not need to be defined. The system will automatically match related variables in it. In the function, you don’t need to worry about whether the $GLOBALS array has been globally defined, you can use it directly.

Similar to the $GLOBALS variable, there is the $php_errormsg string variable. If the track_errors option in the PHP configuration file (php.ini/php3.ini) is turned on, there will be a global variable $php_errormsg where you can see the error information.

In PHP, the effective scope of global variables is limited to the main program and will not affect variables with the same name in functions. That is, global variables and local variables do not infringe each other. If you want variables to penetrate into functions, you need to use the $GLOBALS array or use global definitions.

As for the information entered by the user in the FORM, how should it be processed? It would be nice if in the PHP configuration file, when track_vars is set to On, the variable names are used directly. As in the following example, when next.php is executed, the system will automatically generate two variables, $username and $sex, which can be used directly. Compared with traditional CGI, which has to be parsed by itself, PHP is really amazing.

<form action=<span>next</span>.php method=post><span>
姓名</span>: <input type=text name="username"><br><span>
性别</span>: <input type=text name="sex"><br>
<input type=submit>
</form>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1040155.htmlTechArticlephp study notes (1), php study notes 1. There are two ways to reference files: require and include require The usage method is like require("MyRequireFile.php");. This function is usually placed...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template