Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 2191 of 3366
474 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
669 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
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
372 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
184 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}); } }
369 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
215 Views
PHPStorm can be used to test PHP applications using the PHPUnit testing framework.The PHP interpreter needs to be configured in phpstorm.The Composer should be installed and initialized with respect to the current project.Below are the steps to configure PHPUnit testing −Download phpunit.phar (manually or using the composer) and save it on your machine.The PHPUnit needs to be integrated with a PhpStorm project. PHPUnit can be configured manually or automatically.A PHPUnit test needs to be generated for the class.Once the test has been generated, PHPUnit test methods need to be generated.Post this, the PHPUnit test needs to be debugged and run.Read More
506 Views
The OPCache can be temporarily disabled by adding the below code to the script−ini_set('opcache.enable', 0);This can be used to tell whether OPCache was the reason behind the script failing. Due to this, the user will not have to go through every extension and turn them on/off to see which extension caused the issue.Finding the log that shows which file and what was the reason behind the script failing (when OPCache is enabled).If the user has more information about the application they are trying to debug, this is a feasible option to work on.ini_set('display_errors', 1); error_reporting(~0); If the above two solutions ... Read More
604 Views
Beginning from PHP version 7, it has been made possible to create anonymous classes. Every object in PHP is associated with a class. The anonymous classes can be instantiated to create objects.Example Live DemoOutputDoes the instance belong to parent class? = bool(true)In the above code, a parent class (my_sample_class) has been created, and it has been instantiated with a child class (new class) that inherits from the parent class.We are checking if the instance belongs to the parent class. Since the child class is an extension of the parent class, it returns True as output.
904 Views
Below is the same code to set encoding for FPDI library−Add new fonts that have the correct alphabets.$pdf->AddFont('DejaVu', '', 'DejaVuSansCondensed.php'); $pdf->SetFont('DejaVu', '', 10, '', false);The following are three possible encodings that are possible.cp1250 (Central Europe) cp1251 (Cyrillic) cp1252 (Western Europe) cp1253 (Greek) cp1254 (Turkish) cp1255 (Hebrew) cp1257 (Baltic) cp1258 (Vietnamese) cp874 (Thai) or ISO-8859-1 (Western Europe) ISO-8859-2 (Central Europe) ISO-8859-4 (Baltic) ISO-8859-5 (Cyrillic) ISO-8859-7 (Greek) ISO-8859-9 (Turkish) ISO-8859-11 (Thai) ISO-8859-15 (Western Europe) ISO-8859-16 (Central Europe) Or KOI8-R (Russian) KOI8-U (Ukrainian)Let us see an example to convert the UTF-8 to cp1250.$str = iconv('UTF-8', 'cp1250', 'zazółcić gęślą jaźń');Note− If the string sent ... Read More