php
-
배열을 풀어서 사용해보자 ( extract() )php 2023. 11. 7. 11:25
배열에 저장된 key 와 value를 현재 스코프의 변수로 지정해서 사용할 수 있다 $att = array( 'name' => 'John', 'age' => 30, 'city' => 'New York' ); extract($att, EXTR_SKIP); echo $name; // 'John' echo $age; // 30 echo $city; // 'New York' extract() 를 활용하면 배열안에 key-value 형태로 저장되어있는 데이터를 현재 스코프에서 variable - value 형태로 사용할 수 있다 이때, 기존에 선언되어있던 변수에 덮어씌워지는 것을 주의해햐한다. 2번째 매개변수로 EXTR_SKIP을 선언하면 변수에 덮어쓰지 않고 없는 변수만 추출한다.
-
배열 확인 함수php 2023. 10. 30. 16:17
1. in_array() $fruits = ['apple', 'banana', 'cherry']; if (in_array('banana', $fruits)) { echo '바나나가 배열 안에 있습니다.'; } else { echo '바나나가 배열 안에 없습니다.'; } -> 배열 안에 특정 value가 있으면 true 없으면 false를 반환 2. array_search() $fruits = ['apple', 'banana', 'cherry']; $key = array_search('banana', $fruits); if ($key !== false) { echo '바나나는 키 ' . $key . ' 에 있습니다.'; } else { echo '바나나가 배열 안에 없습니다.'; } -> value를 찾으..
-
class 에서 자기 자신을 instance하기php 2023. 10. 19. 09:11
클래스를 instance할 때, 자기 자신을 instance해서 클래스 내부의 함수를 사용하고싶다면 주의해야할 점이 있다. class Test{ private $testObject; public function __construct() { $this->testObject = new Test(); } } 만일 다음과 같은 형태로 작성되어있는 class를 instance 한다면, 생성된 testObject 내부에서 또 testObject가 생성되는 무한 루프가 되어서 메모리를 소진시켜버린다.. class Test{ private $testObject; public function __construct() { $this->testObject = $this; } } 이를 방지하기 위해서 현재 instance 된..
-
상수php 2023. 9. 14. 08:28
상수 define('NAME', 'Value'); define 함수를 사용해서 상수를 선언할 경우 한번 값이 설정되면 변경할 수 없다. 그리고 PHP 페이지 전역에서 접근할 수 있다. 옵셔널로 불리언은 상수명이 대소문자를 구분하는지 여부를 나타낸다 (구분하면 true , 아니라면 false) -> 상수는 사이트가 동작하는데 필요한 정보를 저장하기 위해 자주 사용되지만, 상수의 값은 사이트가 설치될 때만 값이 변경된다 (사이트가 새로운 서버로 처음 옮겨질 때, 동일한 코드가 다른 웹사이트에서 사용될 때) const NAME = 'Value'; 위의 방식으로 선언하면 클래스 내부에 상수를 정의하는데 사용할 수 있다 (define은 불가능)
-
php 배열 함수php 2023. 9. 13. 09:08
php에서 배열과 관련된 함수 정리 1. 배열에 대한 정보 얻기 array_key_exists($key, $array)-> 배열에서 키를 확인 (true / false 반환) array_search($value, $array[, $strict])-> 배열에 저장된 값을 검색하고 첫번째 키를 반환, $strict = true 라면 타입까지 일치 in_array($value, $array)-> 배열에 어떤 값이 있는지 확인, (true / false 반환) count($array)-> 배열의 항목 개수를 반환 array_rand($array[, $number])-> 배열에서 랜덤한 항목의 키를 반환, 두 번째 매개변수로 숫자 지정 시, 반환할 키의 갯수 지정 2. 배열을 문자로 변환하고 다시 반대로 변환하기..
-
정규 표현식php 2023. 9. 13. 08:32
php 에서 사용하는 정규식에 대해 간단한게 정리 /lst/-> 문자 lst와 일치 /[abcd]/-> 대괄호 내부 문자중 하나 일치 (여기서는 a,b,c,d 중 하나) /[K-Z]/-> 대괄호 안의 하이픈(-)은 일치시킬 문자의 범위 (K ~ Z 사이의 대문자) /[a-e]/-> a 부터 e 사이의 모든 소문자 일치 /[0-9]/-> 0 부터 9 사이의 모든 숫자와 일치 /[A-z0-9]/-> A 부터 Z 사이의 모든 대소문자 또는 0 부터 9 사이의 모든 숫자와 일치 /^[A-Z]/-> 패턴의 시작 부분에 있는 캐럿(^)은 이러한 문자로 시작해야 한다는 것을 지정 (첫 번째 문자가 A~Z 로 시작해야함) /lst\!$/-> 패턴 끝에 있는 달러($) 는 문자열이 지정된 문자로 끝나야함 (문자열의 마지..
-
CI 프레임워크 활용 (주가스크래퍼)php 2023. 9. 12. 19:39
CI는 PHP 언어를 기반으로 만들어진 프레임워크다. MVC 모델을 기반으로 만들어졌으며, 웹 개발부터 일반적인 개발 까지 PHP를 좀 더 편리하게 이용할 수 있게 구성되어있다. 기존에 제작 중이던 주가 스크래퍼를 CI 프레임워크로 migration 하면서 CI의 구성요소를 공부해보았다. 1. index.php // .htaccess 기본적으로 CI 프레임워크는 index.php를 기준으로 시작한다. index 에서는 $system_path , $application_folder의 위치를 찾아주고 FCPATH , BASEPATH 등의 경로 함수를 선언하는 등의 기본 환경 설정을 하고 본격적으로 core/CodeIgniter.php 를 실행한다. index.php를 기준으로 시작하다보니 특별한 조치가 없다..
-