변수 및 변수 범위 | PHP 기초

WBOY
풀어 주다: 2024-07-19 02:18:10
원래의
392명이 탐색했습니다.

Variable & Variable Scope | PHP Fundamentals

Create Variable in PHP

The rules when you create variables in PHP:

  1. Variable declaration with dollar ($) followed by variable name
  2. Variable name must start with a letter or underscore (_)
  3. Variable name is case-sensitive

Valid variables:

$name = "Gunawan"; //valid $Name = "Gunawan"; //valid $_name = "Gunawan; //valid
로그인 후 복사

Not valid variables:

$4name = "Gunawan"; //not valid $user-name = "Gunawan"; //not valid $this = "Gunawan"; //not valid
로그인 후 복사

Variable Scope

PHP has 3 variable scopes:

  1. Global
  2. Local
  3. Static

Global scope

$name = "Gunawan"; function get_name() { echo $name; // not valid } get_name();
로그인 후 복사

To access a global variable within a function you must declare a global variable with the keyword 'global' within a function.

$name = "Gunawan"; function get_name() { global $name; echo $name; // valid } get_name();
로그인 후 복사

Use Array GLOBALS to Access Global Variable

The second way to access global variables is to use a global array.

$name = "Gunawan"; function get_name() { echo $GLOBALS['name']; // valid } get_name();
로그인 후 복사

Static Variable

function test() { static $number = 0; echo $number; $number++; }
로그인 후 복사

Variable Super Global in PHP:

  1. $GLOBALS
  2. $_SERVER
  3. $_GET
  4. $_POST
  5. $_FILES
  6. $_COOKIE
  7. $_SESSION
  8. $_REQUEST
  9. $_ENV

Download my repository php fundamental from my github.

위 내용은 변수 및 변수 범위 | PHP 기초의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:dev.to
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!