PHPコマンド

王林
リリース: 2024-08-29 12:34:20
オリジナル
329 人が閲覧しました

PHP はハイパーテキスト プロセッサの略で、Web アプリケーションを開発するためのサーバー側のスクリプト言語として設計されています。 PHP コードは主に HTML 構文と結合または埋め込まれますが、Web アプリケーションや利用可能な Web フレームワークの任意のテンプレート システムに使用できます。

基本的な PHP コマンド

さまざまな環境で使用できる PHP コマンドが多数あり、特に 1 つの Web アプリケーションを準備したり、サーバー側のコードベース全体を HTML 構文で埋め込んだりする場合に使用でき、通常の開発者にとっては非常に簡単に学習できます。基本的な PHP コマンドのいくつかを以下に示します:

広告 このカテゴリーの人気コース PHP 開発者 - 専門分野 | 8コースシリーズ | 3 つの模擬テスト

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

1. PHP 変数

  • 変数の種類: 変数は、あらゆる種類のプログラミング言語において常に重要な役割を果たします。 PHP では、値を割り当てるために変数の宣言も使用します。 PHP 変数の重要な機能の 1 つは、変数の型を宣言する必要がないことです。 PHP は週次型の言語なので、代入された値に基づいて型を考慮して変数を宣言します。 PHP は通常、文字列、整数、浮動小数点数、ブール値、オブジェクト、リソース、配列、NULL などの変数の種類を受け入れます。
  • 変数の名前: PHP の変数名は常に $ で始まり、その後に任意のテキストまたは特定の文字と _ が続きます。 PHP 変数名は大文字と小文字が区別されるため、同じ名前の大文字の変数はすべて新しい変数とみなされます。
  • 変数のスコープ: 最大の変数はローカル スコープ内にあります。関数内の変数宣言は関数外では使用できません。同様のアプローチで、関数外の変数宣言は関数内では使用できません。 PHP でグローバル変数を宣言することができます。その場合、その変数をグローバルとして明示的に宣言するか、グローバル配列を通じて同じ変数にアクセスする必要があります。

2. PHP オペレーター

  • 代入の演算子: PHP は通常、(‘=’) に等しい 1 つの一般的な演算子を代入に使用します。この等号の左側は変数名で、右側は割り当てられる値になります。
  • 算術演算の演算子: 以下の演算子は、PHP で算術演算を実行するために使用されます。演算子は「+」、「-」、「*」、「/」、「%」、「++」、「–」です。
  • 組み合わせの演算子: 基本的には算術演算子と代入演算子の組み合わせです。結合演算子は「+=」、「-=」、「*=」、「/=」、「%=」です。
  • 比較演算子: 比較演算子は、'=='、'!='、'>'、'>='、'<'、'<=' です。
  • 論理式の演算子: PHP の論理演算子は、「||」、「&&」、「and」、「or」、「xor」、「!」です。

3. PHP If Else

  • 条件判断: プログラミング ロジックのあらゆる種類の条件要件に対して、PHP は他のプログラミング言語と同様に「if else」機能を使用しました。 PHP の「IF ELSE」ステートメントの基本構文は次のとおりです。
IF [SPECIFIC CONDITION]{
[CODE]
}ELSE IF [SPECIFIC CONDITION 2]{
[CODE]
}ELSE {
[CODE]
}
ログイン後にコピー

4. PHP スイッチ

PHP は、複数の「IF ELSE」のネストされた定義を避けるために、他のプログラミング言語と同様に switch case も使用しています。複数のケースを考慮してケースを切り替えます。デフォルトの定義はオプションです。 switch case を定義するコード構造は以下のとおりです:

SWITCH($var){
CASE 'val 1'
[CODE]
Break;
CASE 'val 2'
[CODE]
Break;
CASE 'val 3'
[CODE]
Break;
DEFAULT
[CODE]
}
ログイン後にコピー

5. PHP ループ

  • while ループ: PHP では、言及式が true とみなされるまで while ループを実行できます。
WHILE [condition or expression]{
[CODE]
}
ログイン後にコピー
  • FOR ループ:For ループは、同じコードを言及回数だけ実行するために使用されます。
For(exp 1, exp 2, exp 3){
[CODE]
}
ログイン後にコピー
  • Do While Loop: while ループと同様に、コードは while 式で true 値が取得されるまで実行されます。 while との主な違いは、式が true であるかどうかに関係なく、内側のコードで言及されているコードは少なくとも 1 つを実行しますが、同じであることは保証されないことです。
DO {
[CODE]
}WHILE (condition)
ログイン後にコピー
  • FOREACH ループ: このループは配列を変数として受け入れ、配列の最後の要素までコードを実行することを考慮しています。
FOREACH ($arr_var as $val){
[CODE]
}
ログイン後にコピー

中級 PHP コマンド

他にもいくつかの人気のある PHP コマンドがあり、PHP 開発者によっても使用されています。これらは、あまり基本的なコマンドではありませんが、PHP でよりよく機能します。中間 PHP コマンドのタイプの一部を以下に示します:

1. PHP Include

In PHP, INCLUDE is mainly using for appending define code in an external file with the current working file.

INCLUDE ('name of the external file')
ログイン後にコピー

2. PHP Functions

Maximum business logic can be defined within this PHP function.

Function "name of the function" (argument1, argument2 …){
[CODE]
Return "expected result";
}
ログイン後にコピー

3. PHP Array

Array is mainly holding multiple related information in a single variable. Three kinds of arrays PHP normally supported.

  •  Indexed Array: $student = array(“A”, “B”, “C”);
  • Associative Array: $score = array(“A”=>80, “B”=>90, “C”=>85);
  • Multidimensional Array: $stu_score = array($student, $score);

4. PHP FORM

It is similar to the HTML form.

<form action="" name="" type="post">
ログイン後にコピー

Advanced Commands

However, some of the critical tasks often need to be performed by the users of the PHP command. These tasks also have some advanced commands to execute, such as storing the cookie value, redirecting the page to some relevant pages or connecting to the database. Those advance kind of PHP commands are below:

1. PHP Cookies

A cookie is mainly using storing some of the user type value in their own system so that it can come automatically for the same website.

setCiookie(ckname, ckvalue, ckexpire, ckpath, ckdomain, cksecure) >>> creating Cookie
$_COOKIE['cookie name'] >>> get cookies value
ログイン後にコピー

2. PHP Redirect

Redirecting to a new page by below command:

Header("Location:'URL to redirect'");
ログイン後にコピー

Tips and Tricks to Use PHP Commands

Some common users who are very frequently using PHP commands, they normally use some of the tips and tricks for utilizing PHP commands output in a proper way. Those kinds of tricks normally solving some user-specific queries and display execution output for understanding the same properly. Some of the very commonly used key tricks are:

1. Avoiding multiple ‘IF-ELSE’ statements

For little critical complexity of business logic, sometimes developer using huge ‘IF-ELSE’ condition which creates a real problem of understanding the logic and final review. So one of the popular operators in PHP for avoiding the same is the Ternary operator. It is something like if conditions are true then doing something, else fetching some default value.

$value = (!empty($_GET['val'])? $_GET['val']: 'ABC');
ログイン後にコピー

2. Autoloading of class

Somehow requirement of using some common files in multiple pages, In that case rather than mention those common files in every page, a developer can make one common header and mention those common classes on that header page.

Conclusion

PHP is now a very popular programming language which used by the maximum common web application. If an organization are not a big concern with the site security or code vulnerability then PHP will be always a good option. The population of a page in PHP is very fast rather than any programming language.

以上がPHPコマンドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!