HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🚀
개발 노트
/
🌈
생활코딩
/
🎟️
PHP 기초 문법
🎟️

PHP 기초 문법

 

숫자

var_dump(): 타입

문자열

문자와 문자를 결합할 때에는 .을 사용한다.

변수

변수를 정의할 때는 접두사로 $를 사용한다.

변수의 데이터 형을 조회 및 갱신

gettype, settype

가변변수

변수 이름을 동적으로 변경할 수 있음
 

상수

 

비교

동등 비교 연산자: ==, ≠

입력과 출력

 
notion image
쿼리 파라미터
 

폼

form_get.html
form_get.php
 
notion image
notion image
 

GET vs POST

GET

  • URL의 쿼리 파라미터로 데이터 전송
  • 검색, 특정 상품의 페이지

POST

  • 메시지 body로 데이터 전송
  • 보안성이 더 높다
  • 로그인, 회원가입 등
※ GET이 굳이 필요할까?
⇒ 특정 페이지의 url을 공유할 때 필요하다.
 

조건문

 
 

논리 연산자

&&

좌항과 우항이 모두 true인 경우
 

or

좌항과 우항 중에 하나라도 true인 경우
 

!

부정의 의미. boolean의 값을 역전시킨다.
 

boolean의 대체재

0은 false이고, 0이 아닌 숫자는 true이다.
 

null 체크

  • empty
  • is_null
  • isset: 할당 여부
위의 세 메서드는 세부적으로 차이가 있으니 유의할 것.
PHP type comparison tables
The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at " BlueShoes.
PHP type comparison tables
https://www.php.net/manual/en/types.comparisons.php
PHP type comparison tables
 

느슨한 비교 & 엄격한 비교

  • 느슨한 비교: ==
  • 엄격한 비교: === (타입도 값도 같아야 한다)
PHP type comparison tables
The following tables demonstrate behaviors of PHP types and comparison operators, for both loose and strict comparisons. This supplemental is also related to the manual section on type juggling. Inspiration was provided by various user comments and by the work over at " BlueShoes.
PHP type comparison tables
https://www.php.net/manual/en/types.comparisons.php
PHP type comparison tables
 

반복문

while
for
반복문을 중단시키기 위해 break, continue 사용
 

함수

출력
입력
기본값
 

배열

 
  • count: 배열의 길이를 구할 수 있다.
  • ucfirst: 첫 번째 문자를 대문자로 만든다.
 

배열의 조작

notion image

CRUD

정렬

 
 

느낀점

프로그래밍 언어는 부분적으로 차이는 있으나, 대체적으로 유사하다.
 
var_dump(1) // int(1) var_dump(1.2) // float(1.2)
echo "Hello world" var_dump("hello world") // string(11) // 문자 결합 echo "hello"." "."world"; // hello world
<html> <body> <?php $a = 1; echo $a+1; // 2 print $a+1; // 3 ?> </body> </html>
$a = 100; echo gettype($a); // integer settype($a, 'double'); echo '<br />'; echo gettype($a); // double
$title = 'subject'; $$title = 'PHP tutorial'; echo $subject; // 'PHP tutorial'
define('TITLE', 'PHP Tutorial'); echo TITLE; define('TITLE', 'JAVA Tutorial'); // error
echo 1==2; // bool(false) echo '<br />'; echo 1==1; // bool(true) echo '<br />'; echo 'one'=='two'; // bool(false) echo '<br />'; echo 'one'=='one'; // bool(true) echo '<br />';
echo 'Welcome, '.$_GET['id'];
<html> <body> <form method="get" action="form_get.php"> id : <input type="text" name="id" /> password: <input type="text" name="password" /> <input type="submit" /> </form> </body> </html>
<?php echo $_GET['id'].'.'.$_GET['password']; ?>
<?php if (false) { echo 1; } else if (true) { echo 2; } else { echo 3; } ?>
<?php if ($_GET['id'] === 'egoing') { echo 'right'; } else { echo 'wrong'; } ?>
<?php $i = 0; while($i < 10) { echo 'coding everybody'; $i += 1; } ?>
for ($i = 0; $i < 10; $i++) { echo 'coding everybody'.$i."<br />"; }
for ($i = 0; $i < 10; $i++) { for ($j = 0; $j < 10; $j++) { echo "coding everybody{$i}{$j}<br />"; // 보간 } }
<?php function numbering() { $i = 0; while ($i < 10) { echo $i; $i += 1; } } numbering(); ?>
function get_member1() { return 'kwon'; } function get_member2() { return 'gihong'; } echo get_member1(); // kwon echo ','; echo get_member2(); // gihong
function get_arguments($arg1, $arg2) { return $arg1 + $arg2; } print get_arguments(10, 20); print get_arguments(20, 30);
function get_argument($arg = 100) { return $arg; } echo get_argument(1); // 1 echo ','; echo get_argument(); // 100
#class = ["egoing", "K8805"]; echo class[0]; // egoing echo class[1]; // K8805
function get_members() { return ['egoing', 'k8805', 'sorialgi']; } $members = get_members(); for ($i = 0; $i < count($members); $i++) { echo ucfirst($members[$i]).'<br />'; }
<?php $li = ['a', 'b', 'c', 'd', 'e']; array_push($li, 'f'); array_unshift($li, 'z'); var_dump($li); // ['z', 'a', 'b', 'c', 'd', 'e', 'f'] ?>
<?php $li = ['a', 'b', 'c', 'd', 'e']; array_pop($li); array_shift($li); var_dump($li); // ['b', 'c', 'd'] ?>
$li = ['c', 'e', 'a']; sort($li); var_dump($li); // ['a', 'c', 'e'] rsort($li); var_dump($li); // ['e', 'c', 'a']