Shen Yi's notes on PHP devil training (1), Shen Yi's devil

Source: Internet
Author: User

Shen Yi's notes on PHP devil training (1), Shen Yi's devil

For personal development environments of this course, you can consider using Ubuntu. sublime and PhpStorm are recommended as development environments.
I. What is a major feature of PHP? Can it be run after writing a script without compiling?
But not .......
PHP code must have a "parent" to run ". The most straightforward thing is the compiled "PHP" executable program.
This course is based on/usr/local/php/bin/php.

Ii. Three PHP running Modes

Create a file named xz1024. Why not add a suffix? When you go to the course on your own, the teacher is so capricious, and I am also willful...

<? Phpecho 'My name is xz1024 '. PHP_EOL;?> // If the file only has PHP code, you can leave it empty.

1./usr/local/php/bin/php followed by a php File

Run the above file:/usr/local/php/bin/php xz1024
2./usr/local/php/bin/php-r. "Enter the PHP code here"

Run the above file:/usr/local/php/bin/php-r 'echo "my name is xz1024, 18 ";'

3. Create a file and write it in the first line.
#! /Usr/local/php/bin/php
Then write the code as you like.

Create a file named god.

#!/usr/local/php/bin/php<?php    echo "I am god".PHP_EOL;?>

Then in the command line./god. Press enter. The file does not seem to be executed.

$ Chmod + x god under the command line

OK, let's try again./god

  

Copy it to the/usr/local/bin directory.
Sudo cp god/usr/local/bin (make sure this directory is in your environment variable)

3. How to obtain parameters.

Maybe many people know how to obtain the GET or POST parameters on the webpage? But is there a webpage?
Two global variables $ argc and $ argv appear. The former is the number of parameters (one by yourself), and the latter is the parameter, which is an array.

Below we open the god file of the CP into/usr/local/bin:

    

4. Let's do a small practical drill:

Requirement: When I run god, if there is no parameter, nothing will be displayed. If a parameter exists and the second parameter is-v, the following god version is 1.0 is printed;

#!/usr/local/php/bin/php<?php    $result = '';    if($argc >= 2){     '-v' == $argv[1] && $result = 'god version is 1.0';    }    echo $result;    echo PHP_EOL;

Result:

  

If you are a newbie, please check the manual to add more knowledge points:
1. What is echo?

Echo-output one or more strings. Usage: voidEcho(String$arg1[, String$...])

EchoIt is not a function (it is a language structure), so you do not have to use parentheses to specify parameters, either single quotes or double quotes.Echo(Unlike other languages) does not act like a function, so it cannot always use the context of a function. In addition, if you wantEchoWhen multiple parameters are passed, parentheses are not allowed.

Other references:

  • Print-output string
  • Printf ()-output formatted string
  • Flush ()-refresh the output buffer

2. PHP variable definition:

Variables in PHP are represented by a dollar sign $ followed by the variable name. Variable names are case sensitive.

Variable names follow the same rules as other labels in PHP. A valid variable name must start with a letter or underline followed by any number of letters, numbers, or underscores.


3. PHP_EOL:

Line Break

\ N for unix

Windows \ r \ n

Mac \ r

PHP can be replaced with PHP_EOL to improve the source code portability.

 

4. PHP array:

Arrays in PHP are actually an ordered ing. Ing isValuesAssociatedKeys.

You can use the array () language structure to create an array. It accepts any number of comma-separatedKey => value)Yes.

Array (key => value,...) // The key (key) is an integer or string. // The value (value) can be of any type.
For more information, see www.php.net.

5. $ argc and $ argv

$ Argc-number of parameters passed to the script, including the number of parameters passed to the current script when running in the command line. The script file name is always passed to the current script as a parameter, so$ ArgcThe minimum value is1.

<?phpvar_dump($argc);?> 

$ Argv-an array of parameters passed to the script, including the parameters passed to the current script when running under the command line. The first parameter is always the file name of the current script, so$ Argv [0]Is the script file name.

<?phpvar_dump($argv);?> 

 

7. Conditional judgment if (){}

This will not be written. If not, you really need to look at it from the basics.

 

Copyright statement: the note organizer loves freedom and advocates sharing. However, this note comes from "PHP devil training first stage" by teacher Shen Yi at www.jtthink.com. This learning note was first launched in the blog Park. If you need to repost it, please respect the work of the teacher and keep the signature of the instructor Shen Yi and the course source address.

Next lesson: Shen Yi's notes on PHP devil training (2)

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.