Judging teacher PHP Devil Special Training Note (2), judging Devil _php Tutorial

Source: Internet
Author: User
Tags php definition

Judging teacher PHP Devil Special Training Note (2), judging Devil


One, this lesson will learn a few lazy functions:

1, File_put_contents

(PHP 5, PHP 7)

file_put_contents- Writing a string to a file

Description

int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, Resou Rce $context ]])

and sequentially call fopen (),fwrite (), and the fclose () function.

If filename does not exist, the file is created. Otherwise, the existing file is overwritten, and unless FILE_APPEND the flag is set.

Parameters

    filename

The name of the file to be written to.

    data

The data to write. The type can be a string, anarray, or a stream resource (as mentioned above).

If data specified as a stream resource, the cached data stored in stream here is written to the specified file, which is similar to using the Stream_copy_to_stream () function.

The argument data can be an array (but not a multidimensional array), which is equivalent to file_put_contents ($filename, Join (", $array)).

    flags

  flagsThe value can be a combination of the following flag using the OR (|) operator.

For example (from Php.net)

     $file = 'people.txt';
  // Open the file to get existing content
  $current = file_get_contents($file);
  // Append a new person to the file
  $current .= "John Smith\n";
  // Write the contents back to the file
  file_put_contents($file, $current);
  ?>

2, GETCWD ()//Get current working directory

PHP 4, PHP 5, PHP 7

getcwd- get current working directory

   Description
string getcwd ( void)

Gets the current working directory.

return value

Success returns the current working directory and fails back FALSE .

In some Unix variants, if any parent directory does not have a readable or search mode set,getcwd () will return FALSE even if the current directory is set. For more information about patterns and permissions, see chmod ().

1 For example: in Ubuntu terminal  2 tiger@xz1024:~$ php-r "Echo getcwd ();" 3

3,substr()

(PHP 4, PHP 5, PHP 7)

substr- Returns a substring of a string

Description

string substr ( string $string , int $start [, int $length ])

Returns string a string start length that is specified by the and parameter substring.

Parameters

    string

The input string. Must have at least one character.

    start

If start it is a non-negative number, the returned string starts at string the position starting start at 0. For example, in the string "abcdef", the character at position 0 is "a", the string for position 2 is "C", and so on.

If start it is a negative number, the returned string starts at the end of the first string start character.

If string the length is less than start , FALSE it will be returned.

Example #1 using negative numbersstart

     $rest = substr("abcdef", -1); // 返回 "f"
  $rest = substr("abcdef", -2); // 返回 "ef"
  $rest = substr("abcdef", -3, 1); // 返回 "d"
  ?>

  length

If a positive number is provided length , the returned string start starts at a maximum of length one character (depending on string the length).

If a negative number is provided length , then string many of the characters at the end will be omitted (if start negative numbers are counted from the tail of the string). If start it is not in this text, an empty string is returned.

If a value of 0 FALSE is provided, NULL or length , an empty string is returned.

If not provided length , the returned substring starts at the start position until the end of the string.

Example #2 using negative numberslength

     $rest = substr("abcdef", 0, -1); // 返回 "abcde"
  $rest = substr("abcdef", 2, -1); // 返回 "cde"
  $rest = substr("abcdef", 4, -4); // 返回 ""
  $rest = substr("abcdef", -3, -1); // 返回 "de"
  ?>

Ii. Defining a Custom function

PHP definition functions

 Function name (parameter 1, parameter 2, parameter n)    // must have keyword Funciton{      function body;        }

If you want to return, you ruturn. Forget the return value, it doesn't matter. If the function has a return value, it must be returned.

Three, PHP7 characteristics:

PHP7 allows the return value to be added to the function. such as string, int, array, object, etc.

Function name (): string//Note colon

{

}

Iv. Course Code:

First lesson we set up God this document, this lesson, we build God_func file, through Reuqire in God file introduced function file God_func.

At the same time, in order to learn PHP7 new features, we specifically established GOD_FUNC7 this file, and in God file to judge the introduction.

1. God

# 
 !/usr/local/php/bin/php 
   PHP   require(' God_fun ').  Substr(php_version, 0,1));   $result = "if($argc >=2   ) {'-V ' = =$argv $result = ' God version is 1.0 ';     $argv $result = genconfig ();}   Echo $result ;    Echo Php_eol ;  ? >

2, God_func

 
  PHP  function  genconfig ()  {    returnfile_put_contents( GETCWD ().' /god.json ', ' {} '). ' of bytes is written. ' php_eol. ' God config is created ';  }? >

3, God_func7

 
 1 
   PHP2   function genconfig ():string3   {4      returnfile_put_contents(getcwd(). /god.json ', ' {} '). ' of bytes is written. ' php_eol. ' God config is created '; 5 6    }7 ?>

http://www.bkjia.com/PHPjc/1138632.html www.bkjia.com true http://www.bkjia.com/PHPjc/1138632.html techarticle Judging teacher PHP Devil Special Training Note (2), judging Devil One, this lesson will learn several lazy functions: 1, File_put_contents (PHP 5, PHP 7) file_put_contents write a string ...

  • 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.