PHP

beans_I·2023년 10월 8일

[23-2] 웹프로그래밍

목록 보기
7/13

웹페이지 만들기

# 돔(저번주/ 숙제) stringnum display 등등 date는 제외
3. PHP 파일 읽고 쓰기 
3개의 문제가 출제된다.  

듀얼모니터 사용가능 쉐어링만 안하면 ㅇㅋ

PHP:HyperText Preprocessor

  • 서버 데이터 저장, 이동
  • 파일에 내용을 쓰고 읽고 삭제하는 기능을 구현

왜 PHP?

이지하고 가볍고 다양한 플랫폼과 호환성을 지원하기 때문이다.

php external

echo "hello"; : hello를 화면에 출력

PHP 문법

  1. 대소문자 구별을 하지 않는다.
    echoEcHo등 가능하다.
    1-1. 근데 변수 이름은 구별한다.
  2. 변수의 대한 타입을 설졍하지 않고, $var로 변수를 선언한다.
    ex. $color = "red"
    2-1. (출력문 등에서)변수의 연결은 +가 아닌 .으로 진행한다.
    https://www.w3schools.com/php/phptryit.asp?filename=tryphp_syntax_case2
    2-1-1. 출력문은 echo "I love $txt!";같이 처리도 가능하다.
    https://www.w3schools.com/php/phptryit.asp?filename=tryphp_var2
  3. 주석은 #,/, /**/ 으로 처리가 가능하다.

Loosely Typed Languague
데이터 타입을 그때 결정
7에서는 추가되엇다느데 나중에.

PHP 변수 스코프

local, global, static

! global scope는 다른 언어들과는 다르게, 로컬 변수에 접근하는 것이 불가능하다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_var_global
그렇다고 접근을 못하는 거냐, 하면은 그건 또 아니다.
함수 내에 global x같이x를글로벌변수로사용하겠다고선언해도되고x같이 x를 글로벌 변수로 사용하겠다고 선언해도 되고 GLOBALS['x']로 작성해도 된다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_var_globals


로컬변수는 동일하다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_var_local

static은 한 번 선언이 된 후가 초기화가 안되고 그대로 유지해나가는 변수이다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_var_static

이렇게 하면 0 0 0이 아니라 0 1 2가 출력된다.

PHP Echo와 print의 차이?

echo : 리턴값 x, 처리가 살짝 빠른다.
print : 리터값 o, arg를 받는다.

html 태그를 생성하면서 반환이 가능하다.

데이터 타입

String, integer, float, boolean, array

디버겅 var_dump(); ->어떤 값을 가지고 있는지 확인할 수 있다.
https://www.w3schools.com/php/func_var_var_dump.asp

+) 배열 선언은 ('ㅁ','ㄴ','ㅇ','ㄹ')식으로 진행된다.

객체도 지원한다.

strrev() : reverseOrder

Integer

is_int(): int변수인지 확인. float도 동일
is_infinite() : 값이 넘쳤나
is_nan():
is_numeric() : string여도 숫자만으로 이루어져 있느면 true 출력
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_numbers_numeric

그외 수학 함수들...

타입 캐스팅

(int)$x 같이 타입 캐스팅을 해주면 된다.

상수 저으이

상수는 $로 선언하지 않고 대문자로 쓴다.
define(name(변수 이름), value(변수 값), case_insensitive(대소문자 구별))로 정의가 가능하다. https://www.w3schools.com/php/phptryit.asp?filename=tryphp_constant1
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_constant2

배열 타입도 상수로 정의 가능하다.
상수의 정의는 완전한 글로벌 변수이다.

operator

===, !===은 자바 스크립트와 동일하다.
<=>을 사용하여 -1,0,1을 리턴하는 compareTo를 만들 수 있다.

배열의 더하기도 가능하다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_oper_arr_union
:? 삼항연산자
?? : null나오다가 정상 잡 나오면 리턴 https://www.w3schools.com/php/phptryit.asp?filename=tryphp_oper_null_coalescing

LOOP

foreach :

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_oper_null_coalescing
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_loop_foreach2

함수

배열

인덱스 0,1,2 다른언어랑 동일하다. 당연히 접근도 다른 언어랑 동일하게 가능하다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_num

크기를 구하는 함수로는 count와 sizeof가 있다.

$cars = array("Volvo", "BMW", "Toyota");
echo count($cars) . '<br>'; 
echo sizeof($cars);

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_length

연관배열

딕셔너리 구조라 생각하면 편하다. 다만 "key" => "value"형식으로 작성한다.

사진을 보듯이 foreach문으로 접근이 가능하다.
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_assoc_loop

다차원 배열

array()안에 array()가 있다.
초기화만 array()로 하는거 주의하면 될 듯 하다.

https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_multi
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_multi2

배열의 정렬

sort ( ) - sort arrays in ascending order
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_sort_alpha
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_sort_num

rsort ( ) – sort arrays in descending order
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_rsort_alpha

asort( ) – value값 정렬 arsort()도 있음 https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_asort
ksort( ) – key값 정렬 https://www.w3schools.com/php/phptryit.asp?filename=tryphp_array_ksort

슈퍼글로벌 변수

어떤 범위에서도 접근이 가능하다. 파일을 넘어서도 접근이 가능하다.
$GLOBALS, $_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION

예시) 위에 적어놨던 글로벌 선언 중 하나

  1. $_SEVER[]

    php path 정보 : 자주 씀.

    https://tryphp.w3schools.com/showphp.php?filename=demo_global_server

  1. $.REQUEST[] - get, post등 input을 다 받음
    html form에 담긴 모든 변수를 담고 있다.

    https://tryphp.w3schools.com/showphp.php?filename=demo_global_request
    htmlspecialchars : html 태그로 오인할 문자를 걸러줌
  2. $_POST[] : post만

https://tryphp.w3schools.com/showphp.php?filename=demo_global_post
5. $_GET[] : get만
https://tryphp.w3schools.com/showphp.php?filename=demo_global_get


정규패턴

php에서 뭘 찾을 때 사용한다.

Syntax
/ : the delimiter
w3schools : the pattern that is being searched for
i : a modifier that makes the search case-insensitive.

만족하는 패턴 찾기 -> preg_match() https://www.w3schools.com/php/phptryit.asp?filename=tryphp_regex_match
만족하는 여러 패턴 찾기 -> preg_match_all() https://www.w3schools.com/php/phptryit.asp?filename=tryphp_regex_match_all
값 변환 https://www.w3schools.com/php/phptryit.asp?filename=tryphp_regex_replace
업로드중..

patterns wannachange targetstring

반복 - 그룹핑
https://www.w3schools.com/php/phptryit.asp?filename=tryphp_regex_grouping
ba다음 na가 두번 나오는 / 대소문자 구병 x

profile
노션으로 옮겼습니다. https://beans-i.notion.site/main?pvs=74

0개의 댓글