略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: 预定义常量

2024-11-14

预定义常量

下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。

DIRECTORY_SEPARATOR (string)
PATH_SEPARATOR (string)

注意: PATH_SEPARATOR 是在 PHP 4.3.0-RC2 中引入。

add a noteadd a note

User Contributed Notes 4 notes

up
44
Anonymous
8 years ago
In PHP 5.6 you can make a variadic function.

<?php
/**
* Builds a file path with the appropriate directory separator.
* @param string $segments,... unlimited number of path segments
* @return string Path
*/
function file_build_path(...$segments) {
    return
join(DIRECTORY_SEPARATOR, $segments);
}

file_build_path("home", "alice", "Documents", "example.txt");
?>

In earlier PHP versions you can use func_get_args.

<?php
function file_build_path() {
    return
join(DIRECTORY_SEPARATOR, func_get_args($segments));
}

file_build_path("home", "alice", "Documents", "example.txt");
?>
up
37
Anonymous
8 years ago
For my part I'll continue to use this constant because it seems more future safe and flexible, even if Windows installations currently convert the paths magically. Not that syntax aesthetics matter but I think it can be made to look attractive:

<?php
$path
= join(DIRECTORY_SEPARATOR, array('root', 'lib', 'file.php');
?>
up
-46
-> Anonymous user
3 years ago
<?php
class RegisterController extends Controller{
   
/*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

   
use RegistersUsers;

   
/**
     * Where to redirect users after registration.
     *
     * @var string
     */
   
protected $redirectTo = '/home';

   
/**
     * Create a new controller instance.
     *
     * @return void
     */
   
public function __construct()
    {
       
$this->middleware('guest');
    }

   
/**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
   
protected function validator(array $data)
    {
        return
Validator::make($data, [
           
'name' => 'required|string|max:255',
           
'email' => 'required|string|email|max:255|unique:users',
           
'password' => 'required|string|min:6|confirmed',
        ]);
    }

   
/**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
   
protected function create(array $data)
    {
        return
User::create([
           
'name' => $data['name'],
           
'email' => $data['email'],
           
'password' => bcrypt($data['password']),
        ]);
    }
}
?>

Result:

Success
up
-55
orlov0562 at gmail dot com
6 years ago
While debugging, this function return error number and it's difficult to remember all errors codes, so I think it's should be there.

<?php
print_r
([
   
'PREG_NO_ERROR' => PREG_NO_ERROR,
   
'PREG_INTERNAL_ERROR' => PREG_INTERNAL_ERROR,
   
'PREG_BACKTRACK_LIMIT_ERROR' => PREG_BACKTRACK_LIMIT_ERROR,
   
'PREG_RECURSION_LIMIT_ERROR' => PREG_RECURSION_LIMIT_ERROR,
   
'PREG_BAD_UTF8_ERROR' => PREG_BAD_UTF8_ERROR,
   
'PREG_BAD_UTF8_OFFSET_ERROR' => PREG_BAD_UTF8_OFFSET_ERROR,
]);
?>

Result:

Array
(
    [PREG_NO_ERROR] => 0
    [PREG_INTERNAL_ERROR] => 1
    [PREG_BACKTRACK_LIMIT_ERROR] => 2
    [PREG_RECURSION_LIMIT_ERROR] => 3
    [PREG_BAD_UTF8_ERROR] => 4
    [PREG_BAD_UTF8_OFFSET_ERROR] => 5
)

官方地址:https://www.php.net/manual/en/dir.constants.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3