What is the Ds\Set add() function in PHP?

What is PHP?

PHP is a scripting language that makes web developers able to create effective and interactive web pages that deal with databases. It acts as a competitive alternative to Microsoft ASP. It is an open-source scripting language.

It acts as a core part of WordPress and helps to run the largest social network, Facebook. PHP code runs on the server and the browser receives the output as HTML.

PHP can perform a variety of diverse functions, such as collecting form data, updating the data in a database, creating and updating files on the server, as well as controlling user access. It is able to run on most operating systems and servers, making it highly compatible.

PHP functions

PHP has more than a thousand inbuilt functions and enables its developers to create their own functions as well. Functions are considered one of the extraordinary strengths of PHP. Functions are basically a structure of code that can be used repetitively. Functions need to be called in order to achieve their specific task.

What is a PHP Ds\Set add() function?

The Ds\Set add() function is an inbuilt function used for adding values to the set. This is the syntax for the Ds\Set add() function:

void public Ds\Set::add( $values )

Coded example

The following code adds elements using the add() function and prints all of the elements.

<?php
//empty set
$set = new \Ds\Set();
//Using the add() function to add elements in a set
$set->add("Educative", "for", "PHP", 10, 20);
print_r($set);
?>

Output

Ds\Set Object

  • [0] => Educative
  • [1] => for
  • [2] => PHP
  • [3] => 10
  • [4] => 20

Free Resources