Programming Articles - Page 2189 of 3363

Check whether property exists in object or class with PHP

AmitDiwan
Updated on 30-Dec-2019 06:50:33

3K+ Views

The property_exists() or the isset() function can be used to check if the property exists in the class or object.SyntaxBelow is the syntax of property_exists() function−property_exists( mixed $class , string $property )Exampleif (property_exists($object, 'a_property'))Below is the syntax of isset() function−isset( mixed $var [, mixed $... ] )Exampleif (isset($object->a_property))The isset() will return false if the ‘a_property’ is null.ExampleLet us see an example − Live DemoOutputThis will produce the following output−bool(true) bool(true)

Get all subdirectories of a given directory in PHP

AmitDiwan
Updated on 30-Dec-2019 06:45:37

2K+ Views

To get the subdirectories present in a directory, the below lines of code can be used −Example Live DemoOutputThis will produce the following output. The glob function is used to get all the subdirectories of a specific directory−Array (    [0] => demo.csv    [1] => mark.php    [2] => contact.txt    [3] => source.txt )To get only the directories, the below lines of code can be used−ExampleOutputThis will produce the following output. The glob function is used by specifying that only directories need to be extracted−Array (    [0] => example    [1] => exam    [2] => log )

PHP Regex to get YouTube Video ID

AmitDiwan
Updated on 30-Dec-2019 06:41:43

800 Views

The parse_url and parse_str functions can be used to get the ID of a specific YouTube video.Example Live DemoOutputVX96I7PO8YUIn the above code, the parse_url function takes in a string and slices it into an array of information. The element that the user specifically wants to work with can be specified as a second argument or the entire array can be used.A YouTube video has an ID that can be seen in the URL. The goal is to fetch the ID after the letter ‘v’ and before ‘&’. To accomplish this, the parse_str function can be used. It is similar to GET ... Read More

How to capture the result of var_dump to a string in PHP?

AmitDiwan
Updated on 30-Dec-2019 06:38:35

2K+ Views

The resultant value of var_dumo can be extracted to a string using ‘output buffering’. Below is an example demonstrating the same −Example Live Demo

A Preferred method to store PHP arrays (json_encode or serialize)?

AmitDiwan
Updated on 30-Dec-2019 06:36:39

485 Views

This depends on the requirements in hand.JSON is quicker in comparison to PHP serialization unless the following conditions are met−Deeply nested arrays are stored.The objects that are stored need to be unserialized to a proper class.The interaction is between old PHP versions that don't support json_decode.The below line of code can be used to store PHP arrays using json_encode−json_encode($array, JSON_UNESCAPED_UNICODE)JSON doesn't store the object's original class anywhere, but it can be restored as class instances belonging to stdClass.Why use json_encode instead of serializing?JSON is much more portable in comparison to serialize.The features of __sleep() and __wakeup() can't be leveraged using ... Read More

How to implement reference to an instance method of a particular object in Java?

raja
Updated on 11-Jul-2020 12:53:39

700 Views

Method reference is a simplified form of a lambda expression that can execute one method. It can be described using "::" symbol. A reference to the instance method of a particular object refers to a non-static method that is bound to a receiver.SyntaxObjectReference::instanceMethodNameExample - 1import java.util.*; public class InstanceMethodReferenceTest1 {    public static void main(String[] args) {       String[] stringArray = { "India", "Australia", "England", "Newzealand", "SouthAfrica", "Bangladesh", "WestIndies", "Zimbabwe" };       Arrays.sort(stringArray, String::compareToIgnoreCase);       System.out.println(Arrays.toString(stringArray));    } }Output[Australia, Bangladesh, England, India, Newzealand, SouthAfrica, WestIndies, Zimbabwe]Example - 2@FunctionalInterface interface Operation {    public int average(int ... Read More

Differences between Method Reference and Constructor Reference in Java?

Aishwarya Naglot
Updated on 01-Sep-2025 13:30:13

3K+ Views

The Method Reference and Constructor Reference are part of Java 8's functional programming features, they used for refering to methods and constructors without executing them. They are often used in conjunction with functional interfaces, such as those defined in the java.util.function package. Method Reference A method reference is a shorthand representation of a lambda expression for calling a method. A method reference refers to a method without executing it. Method references can refer to static methods, instance methods, and constructors. Constructor Reference A constructor reference is a unique kind of method reference that is a reference to a constructor. ... Read More

How to strip all spaces out of a string in PHP?

AmitDiwan
Updated on 27-Dec-2019 09:20:30

393 Views

To strip all spaces out of a string in PHP, the code is as follows−Example Live DemoOutputThis will produce the following output−ThisisateststrinTo remove the whitespaces only, the below code can be used−Example Live DemoOutputThis will produce the following output. The str_replace function replaces the given input string with a specific character or string−thisisateststringTo remove whitespace that includes tabs and line end, the code can be used−Example Live DemoHere, the preg_match function is used with regular expressions. It searches for a pattern in a string and returns True if the pattern exists and false otherwise. This will produce the following output−thisisateststringRead More

Is it possible to combine PHP's filter_input() filter flags with AND/OR?

AmitDiwan
Updated on 27-Dec-2019 09:19:05

194 Views

Yes, it is possible to combine filter_input() with AND/OR in PHP. This can be done by looping over the POST fields−$value = filter_input(INPUT_POST, 'field', FILTER_DEFAULT, is_array($_POST['field']) ? FILTER_REQUIRE_ARRAY : NULL);An equivalent for the same user for each loop has been shown below−$memory = array(); //looping through all posted values foreach($_POST as $key => $value) {    //applying a filter for the array    if(is_array($value)) {       $ memory [$key] = filter_input(INPUT_POST, $key, {filters for array});    }    else {       $ memory [$key] = filter_input(INPUT_POST, $key, {filters for scalar});    } }

What are the rules for formal parameters in a lambda expression in Java?

raja
Updated on 11-Jul-2020 12:50:17

383 Views

A lambda expression is similar to a method that has an argument, body, and return type. It can also be called an anonymous function (method without a name). We need to follow some rules while using formal parameters in a lambda expression.If the abstract method of functional interface is a zero-argument method, then the left-hand side of the arrow (->) must use empty parentheses.If the abstract method of functional interface is a one-argument method, then the parentheses are not mandatory.If the abstract method of functional interface is a multiple argument method, then the parentheses are mandatory. The formal parameters are comma-separated and can be in the same order of the ... Read More

Advertisements