Articles on Trending Technologies

Technical articles with clear explanations and examples

Search for partial value match in an Array in PHP

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 1K+ Views

The array_filter function can be used to match a partial value in an array. A callback can be provided, that helps in deciding which elements would remain in the array and which would be removed.When the callback returns false, it means the given element needs to be removed. Below is a code example demonstrating the same −$arr = array(0 => 'abc', 1 => 'def', 2 => 'ghijk', 3 => 'lmnxyz'); $results = array(); foreach ($arr as $value) {    if (strpos($value, 'xyz') !== false) { $results[] = $value; } } if( empty($results) ) { echo 'No matches found.'; } else ...

Read More

How to create Web API service in PHP?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 941 Views

SOAP and REST APIs are the widely used APIs.Consider the presence of a PHP class named manage.php that helps in managing the entries in a database.class manage { private $entryId; function __construct($entryId) {    $this->entryId = $entryId; } function deleteEntry() {    //delete $this->entryId from database }}On the server, this functionality can be accessed as shown below −require_once('manage.php'); $m = new manage(12); $m->deleteEntry();How can this be accessed by a different server? A third file can be created that will behave like a buffer/an interface that helps access this data. Below is a sample buffer −Let us call it ‘api/delete.php’require_once('manage.php'); if(hasPermission($_POST['api_key']) ...

Read More

PHP file that should run once and delete itself. Is it possible?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 321 Views

Yes, it can be done using the unlink function. It has been shown below −Another alternative that deletes the script irrespective of whether the exit function is called or not, has been shown below ^minus;class DeleteOnExit {    function __destruct() {       unlink(__FILE__);    } } $delete_on_exit = new DeleteOnExit();

Read More

How to use RegexIterator in PHP?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 372 Views

Regular expression$directory = new RecursiveDirectoryIterator(__DIR__); $flattened = new RecursiveIteratorIterator($directory); // Make sure the path does not contain "/.Trash*" folders and ends eith a .php or .html file $files = new RegexIterator($flattened, '#^(?:[A-Z]:)?(?:/(?!\.Trash)[^/]+)+/[^/]+\.(?:php|html)$#Di'); foreach($files as $file) {    echo $file . PHP_EOL; }Using filtersA base class holds the regex that needs to be used with the filter.classes which will extend this one. The RecursiveRegexIterator is extended.abstract class FilesystemRegexFilter extends RecursiveRegexIterator {    protected $regex;    public function __construct(RecursiveIterator $it, $regex) {       $this->regex = $regex;       parent::__construct($it, $regex);    } }They are basic filters and work ...

Read More

Create nested JSON object in PHP?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 2K+ Views

JSON structure can be created with the below code −$json = json_encode(array(    "client" => array(       "build" => "1.0",       "name" => "xxxx",       "version" => "1.0"    ),    "protocolVersion" => 4,    "data" => array(       "distributorId" => "xxxx",       "distributorPin" => "xxxx",       "locale" => "en-US"    ) ));

Read More

Add PHP variable inside echo statement as href link address?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 4K+ Views

HTML in PHPecho "Link";orecho "Link";PHP in HTML

Read More

How do I sort a multidimensional array by one of the fields of the inner array in PHP?

AmitDiwan
AmitDiwan
Updated on 07-Apr-2020 292 Views

The usort function can be used to sort a multidimensional array. It sorts with the help of a user defined function.Below is a sample code demonstration −Examplefunction compare_array($var_1, $var_2) {    if ($var_1["price"] == $var_2["price"]) {       return 0;    }    return ($var_1["price"] < $var_2["price"]) ? -1 : 1; } usort($my_Array,"compare_array") $var_1 = 2 $var_2 = 0OutputThis will produce the following output −1Explanation − We have declared var_1 and var)2 with integer values. They are compared and the result is returned.

Read More

How to reset the JShell session in Java 9?

raja
raja
Updated on 07-Apr-2020 640 Views

Java 9 has introduced JShell for Java, and it allows us to evaluate code snippets such as declarations, statements, and expressions.During the JShell session, we need to reset it without closing and re-opening JShell then we can use the internal command: "/reset". By using this command, code entered during the current session has erased. It can be useful when we want to test new classes, create new variables, etc. while keeping the names previously used.In the below snippet, we have created variables x, y, and str. We can able to see all entered code snippets using the "/list" command. After that, we can apply ...

Read More

PHP string cast vs strval function, which one should I use?

AmitDiwan
AmitDiwan
Updated on 06-Apr-2020 1K+ Views

A value can be converted into a string with the help of (string) cast or the strval() function.The strval() function is a function call whereas (string) cast is an internal type casting method.Unless there is some specific dataset or use case, both of these can be used interchangeably.This is because PHP uses automatic type conversion, due to which a variable's type is determined based on the context in which it is used.The strval($var) function returns the string value of $var whereas the (string)$var explicitly converts the "type" of $var during the process of evaluation.The $var can be any scalar type ...

Read More

What are the different feedback modes in JShell in Java 9?

raja
raja
Updated on 06-Apr-2020 264 Views

When performing an operation in the JShell tool, it displays a message in return (success of the command, error, and type of a variable created as well as its value). It has been customized using the command: "/set feedback". This command displays the type of return currently configured as well as the different return modes available.jshell> /set feedback | /set feedback normal | | Available feedback modes: | concise | normal | silent | verboseThere are four feedback modes available in JShell as listed below:1) /set feedback normal: This is the default JShell feedback. When we evaluate an expression, JShell returns the corresponding result and an ...

Read More
Showing 51011–51020 of 61,248 articles
Advertisements