728x90

PHP 스크립트(script) 실행(execution)시 테스트 등의 목적으로 실행을 지연(delay)시켜야할 경우가 있습니다. 이때 사용할 수 있는 함수 입니다.

 

 

1. 초 단위로 지연

 

int sleep ( int $seconds )

 

$seconds로 주어진 초 만큼 실행을 지연합니다. 성공시 0을 반환, 실패시 FALSE를 반환합니다. 음수를 지정하면 오류입니다. Warning이 발생합니다.

 

<?php
// ...
// 2초간 지연합니다.
sleep(2);
//...
?>

 

 

2. 마이크로초 단위로 지연

 

void usleep ( int $micro_seconds )

 

$micro_seconds로 주어인 마이크로 초(백만분의 1초) 만큼 실행을 지연합니다. 값을 반환하지 않습니다. 음수를 지정하면 오류입니다. Warning이 발생합니다.

 

<?php
// ...
// 2초간 지연합니다.
usleep(2000000);
// ...
?>

 

 

3. 지정된 시간까지 지연

 

bool time_sleep_until ( float $timestamp )

 

$timestamp 로 주어진 시간까지 지연합니다. 성공시 TRUE, 실패시 FALSE를 반환합니다. 과거 시간을 지정하면 오류입니다. Warning이 발생합니다.

 

<?php // 2초 지연 time_sleep_until(time() + 2); // 0.2초 지연 time_sleep_until(microtime(true) + 0.2); ?>

 

※ 참고

- int time(void) : January 1 1970 00:00:00 GMT 부터 지금까지의 초를 반환합니다.

- mixed microtime ([ bool $get_as_float = false ] ) : 현재의 타임스탬프를 마이크로초로 반환합니다. 인자로 주어지는 $get_as_float가 TRUE이면 마이크로초에 가장 근접한 초값을 float 타입으로 반환하고, FALSE 이면 string 타입의 값을 반환합니다.

 

PHP 프로그램을 지연시키는 다양한 방법을 알아보았습니다.



출처: https://offbyone.tistory.com/197 [쉬고 싶은 개발자]

728x90

Class 'ZipArchive' not found 에러가 발생하는 이유는 zip 모듈이 없어서이다.

phpinfo() 하여서 확인해보면 알 수 있다.

 

모듈을 설치하려면 PHP 다시 컴파일하거나 모듈만 설치하는 방법이 있다.

귀찮으니 모듈만 따로 설치하자.

Zip 모듈은 pecl 에서 다운로드 받으면 된다. 여기서 Stable 버전을 다운받는다.

http://pecl.php.net/package/zip

 

wget http://pecl.php.net/get/zip-1.10.2.tgz

압축을 해제하고

tar xvfz zip-1.10.2.tgz

cd zip-1.10.2

phpize     (혹시 phpize: command not found 라고 나오면 yum install php-devel  한후 다시 실행)

./configure --with-php-config=/usr/local/php/bin/php-config --enable-zip && make

cd modules/

ls 를 하면 zip.la  zip.so 가 보인다.

zip.so를 php 확장 디렉토리로 복사한다.

cp zip.so /usr/local/php/lib/php/extensions

php 설정파일을 열어서 zip.so 부분을 추가해준다.

vi /etc/php.ini

extension_dir="/usr/local/php/lib/php/extensions"

extension="zip.so" 를 추가.

/usr/local/apache2/bin/apachectl restart



출처: https://mara.tistory.com/327 [Absolute Purpose]

 

In CentOS 7.4 

yum install php-pecl-zip

 

728x90

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

mb_substr  Get part of string

Description 

mb_substr(
    string $string,
    int $start,
    ?int $length = null,
    ?string $encoding = null
): string

Performs a multi-byte safe substr() operation based on number of characters. Position is counted from the beginning of string. First character's position is 0. Second character position is 1, and so on.

Parameters 

string

The string to extract the substring from.

start

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

If start is negative, the returned string will start at the start'th character from the end of string.

length

Maximum number of characters to use from string. If omitted or NULL is passed, extract all characters to the end of the string.

encoding

The encoding parameter is the character encoding. If it is omitted or null, the internal character encoding value will be used.

Return Values 

mb_substr() returns the portion of string specified by the start and length parameters.

Changelog 

VersionDescription

8.0.0 encoding is nullable now.

See Also 

https://www.php.net/manual/en/function.mb-substr

 

728x90

Super global variables are built-in variables that are always available in all scopes.


PHP $_REQUEST

PHP $_REQUEST is a PHP super global variable which is used to collect data after submitting an HTML form.

The example below shows a form with an input field and a submit button. When a user submits the data by clicking on "Submit", the form data is sent to the file specified in the action attribute of the <form> tag. In this example, we point to this file itself for processing form data. If you wish to use another PHP file to process form data, replace that with the filename of your choice. Then, we can use the super global variable $_REQUEST to collect the value of the input field:

Example

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_REQUEST['fname'];
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>


</body>
</html>

+ Recent posts