반응형

addslashes

(PHP 4, PHP 5)

addslashes — Quote string with slashes

Description ¶

string addslashes ( string $str )

페이지에서 json 으로 parsing 하는 일이 있어서 작업을 하던 중.

텍스트에 double quote 가 들어가 있는 경우 오류를 발생하여... 


php 에서 값을 셋팅할 때부터 '\' 를 붙여서 처리하게 되면 문자를 인식하게 되는 것을 확인 한 후에

str_replace 를 사용하여 간단히 하려 하였으나 addslashes 라는 더 좋은 함수를 알게 되었다.


str_replace('"', "\"", $my_string);  을 addslashes($my_string); 로 하게 되면 double/single queto 뿐만 아니라 '\' 와 NUL 까지 처리해 준다는 사실!!!



참고)

http://kr1.php.net/manual/en/function.str-replace.php

http://kr1.php.net/manual/en/function.addslashes.php


반응형
반응형

money_format() 을 쓰려다가 number_format() 있길래 사용함.


number_format

(PHP 4, PHP 5)

number_format — 천의 단위로 숫자를 포맷팅한다.

Description ¶

string number_format ( float $number [, int $decimals = 0 ] )
string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

이 함수는 1개나 2개 또는 4개의 파라미터만 사용한다.(3개 안됨):

한 파라미터만 주어진다면, number (첫번째 param)  decimals(2번째 param) 없이 포맷팅 될 것임, 여튼 천 단위별로 콤마(,)가 찍힐거다.

두 개의 파라미터가 주어진다면, number 는 decimals dot (".") 으로 구분되는 소숫점을 가진, 천의 단위로 콤마를 가질 것이다.

4개의 파라미터가 주어진다면, number will be formatted with decimals decimals, dec_point instead of a dot (".") before the decimals andthousands_sep instead of a comma (",") between every group of thousands.

Parameters ¶

number

The number being formatted.

decimals

Sets the number of decimal points.

dec_point

Sets the separator for the decimal point.

thousands_sep

Sets the thousands separator.

그래서 ... 확인해보니 number_format($number, '0', '.', ','); 랑 number($number) 가 기본 숫자 사용 시에는 같은 결과를 나타냈다.

반응형
반응형

1. PHP rand() vs mt_rand()


둘다 PHP 에서 난수를 발생시켜주는 함수입니다. 이름을 보면 알겠지만 당연히 rand() 가 먼저 사용되고 있었구요.


 먼저 기본적인 사용은 rand(), mx_rand() 를 호출하거나 시작값과 끝값을 주어 rand(1,10) , mt_rand(1,10) 과 같이 동일하게 사용하면 됩니다. 


 이렇게 외형적으로는 비슷하나 똑똑한 개발자들이 mt_rand() 를 추가적으로 만든 이유가 있을텐데 그 이유에 대해서 알아보도록 하겠습니다. 앞서 기능을 살펴보면 rand() 와 mt_rand() 는 모두 0부터 시스템에서 제공하는 범위의 난수를 반환하는데(각각 getrandmax(), mt_getrandmax() ), rand() 의 경우엔 운영체제에 따라 max 의 범위가 32,767 이라고 합니다. 따라서 더 큰 수를 사용해야 한다면 mt_rand() 를 사용해야 합니다.


 그리고 내부적인 알고리즘에서 차이가 있습니다. rand() 의 경우에는 LCG(Linear Congruential Generator) 를 사용하고, mt_rand() 는 Mersenne Twister 를 사용합니다. 각각 생성가능한 수의 범위는 다음과 같으며  232  , 219937 − 1  속도 또한 4배나 빠르다고 합니다.


 그래서 아래와 같이 테스트를 해 보았습니다.


<?php

    $mt_rand_start = microtime(true);

    for ($i=0; $i<1000000; $i++)

    {

        $tm_random = mt_rand(0, mt_getrandmax());

    }

    $mt_rand_end = microtime(true);

    $runtimeMtRand= $mt_rand_end - $mt_rand_start;

    echo "Runtime mt_rand(): ".$runtimeMtRand." seconds! \n";


    $rand_start = microtime(true);

    for ($i=0; $i<1000000; $i++)

    {

        $random = rand(0, getrandmax());

    }

    $rand_end = microtime(true);

    $runtimeRand= $rand_end - $rand_start;

    echo "Runtime rand(): ".$runtimeRand." seconds! \n";

?>


결과는... 이론과는 달랐네요. ㅠㅠ 왜?


 실제 사용 시 난수 발생은 rand() 보다 mt_rand() 가 더 확실한 random() 범위를 나타내나 이상하게 속도에서는 앞서거니 뒤서거니 하는 결과를 보였습니다. 인터넷 검색을 해보니 뭐 시스템에 따란 다른 결과를 보이느니 하는데요. 4배까지는 아니더라도 평균적으로 빠른 결과를 보이는 것 같습니다. 



참조>

http://us1.php.net/manual/en/function.rand.php

http://us1.php.net/manual/en/function.mt-rand.php

http://en.wikipedia.org/wiki/Linear_congruential_generator

http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html

http://golearnphp.com/php-rand-vs-mt_rand-and-openssl_random_pseudo_bytes/


반응형
반응형

1. php 문자열에서 lastIndexOf 가 없는 것 같아 다른 함수들을 이용하여 만들어 봤다.

public function lastIndexOf($str, $cmp)
{
    return strrev(substr(strrev($str),strpos(strrev($str),$cmp)));
}

반응형
반응형

1. 작은 따옴표와 큰 따옴표의 사용.
 a. 예시
  1) $info='Preface variables with a $ like this : $variable'; 
  2) echo "There have been $count presidents of the US";
 b. 작은 따옴표 : 정확한 내용을 유지하는 문자 그대로의 문자열을 할당하기 위함.
 c. 큰 따옴표 : 문자열 안의 $ 사용을 변수로 인지함.

2. 다중 라인 명령어.
 - <<<_END ... ... _END; 의 사용.
 - $out = <<<_END a
   b
   c
   _END;

반응형
반응형
PHP 5.3.10 Released! - PHP 보안관련 버그가 고쳐졌다고 하네요.
 
http://t.co/40ne5QN4

다음의 코드가 실행된다면?? 
http://t.co/6GSwO3n9 ㅎㄷㄷ 할듯-

바로업뎃!~~
반응형
반응형


Yii Framework


 Yii 프레임워크는 웹2.0 어플리케이션을 개발하는데 최고의 고성능 PHP 프레임워크입니다. 오픈 소스 기반의 Yii 프레임워크는 PHP5로 쓰여졌고  DRY design 과 rapid development 를 채택하고 있습니다. 그리고 테스트와 디버그를 할 수 있도록 페키지로 되어 있는 것과 빠르며 보안 및 전문적인 특성을 가지고 있습니다.


Credits

아래는 Yii 과 관련있는 또는 영감을 준 잘 알려진 웹 프로그래밍 프레임워크의 목록 입니다.
-  
PradoRuby on RailsjQuerySymfonyJoomla


Featrues of Yii

- Model-View-Controller(MVC) design pattern
- Database Access Objects(DAO), Query Builder, Active Record, DB Migration
- Form input and validation 
- AJAX-enabled widgets
- Authentication and authorization
- Skinning  and theming
- Web servieses
- Internationalization(I18N) and localization(L10N)
- Layered caching scheme
- Error handling and logging
- Security
- Unit and functionality testing
-  Automatic code generation
- Compliance to XHTML
- Purely object-oriented
- Friendly with third-party code
- Detailed documentation
- Extension library


Performance of Yii

아래의 그림이 Yii 프레임워크가 좋은 이유를 대변해 줍니다. ㅎ;

 
결론적으로 이렇게 빠른 이유는 
 lazy loading technique 을 사용하기 때문입니다.

License of Yii Framework

Yii Framework 는 프리웨어이며 BSD License 를 따릅니다.


 
반응형

+ Recent posts