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

Source: Internet
Author: User
Tags php 7 features

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

1. This course will learn a few lazy functions:

1、file_put_contents

(PHP 5, PHP 7)

File_put_contents-write a string to a file

Description int File_put_contents(String $filename, Mixed $data[, Int $flags= 0 [, resource $context])

It is the same as calling the fopen (), fwrite (), and fclose () functions in turn.

IffilenameDoes not exist, the file is created. Otherwise, the existing file is overwritten, unlessFILE_APPENDFlag is set.

Parameters
    filename

The name of the file to be written.

    data

The data to be written. The type can be string, array, or stream resources (as mentioned above ).

IfdataIf it is specified as a stream resource, the cached data stored in the stream will be written to the specified file. This usage is similar to using the stream_copy_to_stream () function.

ParametersdataIt can be an array (but not a multi-dimensional array), which is equivalentFile_put_contents ($ filename, join ('', $ array )).

    flags

  flagsThe value can be the following flag OR (|) Operator.

For example(Derived from P HP.net)

  <?php
  $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 the current working directory
 

PHP 4, PHP 5, PHP 7

Getcwd-get the current working directory

   Description
String Getcwd(Void)

Obtain the current working directory.

Return Value 

If the job is successful, the current working directory is returned. If the job failsFALSE.

In some Unix variants, if no parent directory is set to readable or search mode, even if the current directory is set,Getcwd ()Will returnFALSE. For more information about modes and permissions, see chmod ().

1 example: In ubuntu Terminal 2 tiger @ xz1024 :~ $ Php-r "echo getcwd ();" 3/home/tigertiger @ xz1024 :~ $

 

3、substr()

(PHP 4, PHP 5, PHP 7)

Substr-returns the substring of the string.

Description
String Substr(String $string, Int $start[, Int $length])

Returns a string.stringBystartAndlengthThe substring specified by the parameter.

Parameters
    string

Input string. It must contain at least one character.

    start

IfstartThe return string is fromstringOfstartStart from 0. For example, in the string"Abcdef", In the location0The character is"A", Location2The string is"CAnd so on.

IfstartIs a negative number, and the returned string isstringForward number at the endstartCharacters.

IfstringThe length is lessstart, Will returnFALSE.

Example #1 use a negative numberstart

<? Php
$ Rest = substr ("abcdef",-1); // return "f"
$ Rest = substr ("abcdef",-2); // return "ef"
$ Rest = substr ("abcdef",-3, 1); // return "d"
?>

  length

If a positive number is providedlength, The returned string will bestartWhich can start at mostlengthCharacters (depending onstringLength ).

If a negativelength, ThenstringMany characters at the end will be missed (ifstartIs a negative number, which is counted from the end of the string ). IfstartOtherwise, an empty string is returned.

If the value is0,FALSEOrNULLOflengthReturns an empty string.

If nolength, The returned substring isstartThe position starts until the end of the string.

Example #2 use a negative numberlength

<? Php
$ Rest = substr ("abcdef", 0,-1); // return "abcde"
$ Rest = substr ("abcdef", 2,-1); // return "cde"
$ Rest = substr ("abcdef", 4,-4); // return ""
$ Rest = substr ("abcdef",-3,-1); // return "de"
?>

 

Ii. Define a UDF

PHP-defined functions

Function Name (parameter 1, parameter 2, parameter n) // you must have the keyword funciton {function body ;}

If you want to return, ruturn. It doesn't matter if you forget the return value. If a function returns a value, it must be returned.

Iii. php 7 features:

PHP 7 allows adding return values to functions. Such as string, int, array, object, etc.

Function Name (): string // note the colon

{

}

Iv. course code:

In the first lesson, we created the GOD file. In this lesson, we created the GOD_FUNC file and introduced the function file god_func in the god file through reuqire.

At the same time, in order to learn the new features of PHP 7, we create the god_func7 file and judge and introduce it in the god file.

1. god

#! /Usr/local/php/bin/php <? Php require ('God _ fun '. substr (PHP_VERSION,); // after determining the PHP version, introduce different god_func $ result = ''; if ($ argc> = 2) {'-V' = $ argv [1] & $ result = 'God version is 1.0 '; 'init '= $ argv [1] & $ result = genConfig ();} echo $ result; echo PHP_EOL;?>

2. god_func

<?php  function genConfig()  {    return file_put_contents(getcwd().'/god.json','{}').' of bytes is written.'.PHP_EOL.'god config is created';  }?>

3. god_func7

1 <?php2   function genConfig():string3    {4     return file_put_contents(getcwd().'/god.json','{}').' of bytes is written.'.PHP_EOL.'god config is created';5 6    }7 ?>

 

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.