php study notes (1)

WBOY
Release: 2016-08-08 09:19:35
Original
884 people have browsed it

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

 The use of require is such as 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.

 The usage method of include is as follows: include("MyIncludeFile.php");. This function is generally placed in the processing part of flow control. The PHP program webpage 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

Comments: The information explained is what and why.

3. Constant type

  PHP defines the following constants in constants.

 __FILE__
 This default constant is the PHP program file name. 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 that executes the PHP parser, such as 'Linux'.

 TRUE
 This constant is the truth value (true).

  FALSE
 This constant is the 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 in parsing grammar.

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

 For these constants starting with E_, you can 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 the 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 retain a global variable in the memory Area. 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 FORM, how to process it? It would be nice if in the PHP configuration file, when track_vars is set to On, the variable name is used directly. As shown 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

The above introduces the PHP learning notes (1), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!