PHP如何使用Ds Stack push()函数?代码示例

2021年3月25日11:59:22 发表评论 727 次浏览

Ds \ Stack :: push()PHP函数用于在末尾添加元素。也就是说, 它用于将元素压入栈。被推送的元素可以是混合数据类型。

语法如下:

void public Ds\Stack::push ($values)

参数:此函数接受单个参数$值用于推入栈。

返回值:此函数不返回任何值。

下面的程序说明了Ds \ Stack :: push()功能:

程序1:

<?php
  
// PHP program to illustarte the 
// push() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
  
// Print the stack
print_r( $stack );
  
?>

输出如下:

Ds\Stack Object
(
    [0] => GfG
    [1] => to
    [2] => Welcome
)

程序2:

<?php
  
// PHP program to illustarte the 
// push() function 
  
// Create a Stack instance
$stack = new \Ds\Stack();
  
// Pushing Mixed value elements to Stack
$stack ->push( "Welcome" );
$stack ->push( "to" );
$stack ->push( "GfG" );
$stack ->push(10);
$stack ->push(5.5);
  
  
// Print the stack
print_r( $stack );
  
?>

输出如下:

Ds\Stack Object
(
    [0] => 5.5
    [1] => 10
    [2] => GfG
    [3] => to
    [4] => Welcome
)

参考:https://www.php.net/manual/en/ds-stack.push.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: