ホームページ  >  記事  >  バックエンド開発  >  PHP スタック トレース

PHP スタック トレース

王林
王林オリジナル
2024-08-29 13:05:26446ブラウズ

特定のプロパティが関連付けられた要素の連続したコレクションは、PHP ではスタックと呼ばれます。また、スタックは後入れ先出しベースで動作します。つまり、スタック内で最後に配置されたオブジェクトがスタックから削除される最初のオブジェクトとなり、スタックへの要素の追加と要素の削除はすべてスタックの一端のみに制限されます。

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

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

PHP でスタックを宣言する構文は次のとおりです:

push(item_to_added_to_the_stack);
pop();

ここで、 item_to_be_added_to_the_stack は、スタックの先頭からスタックに追加されるアイテムです。

PHP におけるスタックの働き

PHP におけるスタックの動作は次のとおりです:

  • スタックは後入れ先出しベースで動作します。つまり、スタック内で最後に配置されたオブジェクトが、スタックから最初に削除されるオブジェクトになります。
  • スタックを定義する操作はプッシュとポップです。
  • スタックのプッシュ操作とは、スタックの先頭から要素をスタックに追加することを意味します。
  • スタックのポップ操作は、スタックの要素をスタックの先頭から削除することを意味します。

PHP スタック トレースの例について説明します。

例 #1

push() 関数と Pop() 関数を使用してスタックに項目を追加し、スタックの先頭から項目を削除し、スタックの内容を表示する PHP プログラム:

コード:

<?php
//creating an instance of SplQueue class
$newstack = new SplQueue();
//using push() function to the add items to the stack from the top of the stack
$newstack->push('Welcome');
$newstack->push('to');
$newstack->push('PHP');
//printing the contents of the stack after push operation in a human readable format by using print_r function
echo "The elements present in the stack after push operation are:\n";
print_r ($newstack);
//Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function
$newstack->pop();
$newstack->pop();
echo "The elements present in the stack after pop operation are:\n";
print_r ($newstack);
?>

出力:

PHP スタック トレース

次に、push() 操作を使用して、スタックの先頭から要素をスタックに追加します。次に、スタックの内容を出力として画面に表示します。次に、pop() 操作を使用して、スタックの先頭から要素を削除します。次に、スタックの内容を出力として画面に表示します。

例 #2

push() 関数と Pop() 関数を使用してスタックに項目を追加し、スタックの先頭から項目を削除し、スタックの内容を表示する PHP プログラム:

コード:

<?php
//creating an instance of SplQueue class
$newstack = new SplQueue();
//using push() function to the add items to the stack from the top of the stack
$newstack->push('Learning');
$newstack->push('is');
$newstack->push('fun');
//printing the contents of the stack after push operation in a human readable format by using print_r function
echo "The elements present in the stack after push operation are:\n";
print_r ($newstack);
//Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function
$newstack->pop();
$newstack->pop();
$newstack->pop();
echo "The elements present in the stack after pop operation are:\n";
print_r ($newstack);
?>

出力:

PHP スタック トレース

次に、push() 操作を使用して、スタックの先頭から要素をスタックに追加します。次に、スタックの内容を出力として画面に表示します。次に、pop() 操作を使用して、スタックの先頭から要素を削除します。次に、スタックの内容を出力として画面に表示します。

例 #3

push() 関数と Pop() 関数を使用してスタックに項目を追加し、スタックの先頭から項目を削除し、スタックの内容を表示する PHP プログラム:

コード:

<?php
//creating an instance of SplQueue class
$newstack = new SplQueue();
//using push() function to the add items to the stack from the top of the stack
$newstack->push('We');
$newstack->push('love');
$newstack->push('India');
//printing the contents of the stack after push operation in a human readable format by using print_r function
echo "The elements present in the stack after push operation are:\n";
print_r ($newstack);
//Removing two items from the top of the stack using pop() function and then displaying the contents of the stack in human readable form using print_r function
$newstack->pop();
echo "The elements present in the stack after pop operation are:\n";
print_r ($newstack);
?>

出力:

PHP スタック トレース

次に、push() 操作を使用して、スタックの先頭から要素をスタックに追加します。次に、スタックの内容を出力として画面に表示します。次に、pop() 操作を使用して、スタックの先頭から要素を削除します。次に、スタックの内容を出力として画面に表示します。

結論

この記事では、定義、構文、スタックを定義する基本操作、つまりプログラミング例とその出力を通じて、PHP の Push() 関数と Pop() 関数を通じて、PHP におけるスタックの概念を学びました。

以上がPHP スタック トレースの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
前の記事:ソート文字列 PHP次の記事:ソート文字列 PHP