Friday, March 29, 2019

String comparison using == vs strcmp() in PHP || How to use strcmp() in PHP

The comparison operator called Equal Operator is the double equal sign “==”. This operator accepts two inputs to compare and returns true value if both of the values are same (It compares only value of variable, not data types) and returns false value if both of the values are not same.
This should always be kept in mind that the present equality operator == is different from the assignment operator =. The assignment operator assigns the variable on the left to have a new value as the variable on right, while the equal operator == tests for equality and returns true or false as per the comparison results.

<?php 

// Declaration of strings 
$name1 = "Geeks"; 
$name2 = "Geeks"; 

// Use == operator 
if ($name1 == $name2) { 
echo 'Both strings are equal'; 
else { 
echo 'Both strings are not equal'; 

?> 

Both strings are equal
strcmp() Function

The strcmp() is an inbuilt function in PHP which is used to compare two strings. This function is case-sensitive which points that capital and small cases will be treated differently, during comparison. This function compares two strings and tells whether the first string is greater or smaller or equals to the second string.
Syntax:
strcmp( $string1, $string2 )
Parameters: This function accepts two parameters as mentioned above and described below:
  • $string1: This parameter refers to the first string to be used in the comparison. It is mandatory parameter.
  • $string2: This parameter refers to the second string to be used in the comparison. It is mandatory parameter.
Return Values: The function returns a random integer value depending on the condition of match, which is given by:
  • Returns 0 if the strings are equal.
  • Returns a negative value (< 0), if $string2 is greater than $string1.
  • Returns a positive value (> 0) if $string1 is greater than $string2.
<?php 

// Declaration of strings 
$name1 = "Geeks"; 
$name2 = "geeks"; 

// Use strcmp() function 
if (strcmp($name1, $name2) !== 0) { 
echo 'Both strings are not equal'; 
else { 
echo 'Both strings are equal'; 
?> 

Convert PHP to XML || How to convert array to SimpleXML in PHP

Many times need to store the data as a XML format into the database or into the file for later use. To fulfill this requirement need to convert data to XML and save XML file.
The SimpleXML extension functions provides the tool set to convert XML to an object. Those objects deals with normal property selectors and array iterators.

<?php
// Code to convert php array to xml document
// Define a function that converts array to xml.
function arrayToXml($array, $rootElement = null, $xml = null) {
$_xml = $xml;
// If there is no Root Element then insert root
if ($_xml === null) {
$_xml = new SimpleXMLElement($rootElement !== null ? $rootElement : '<root/>');
}
// Visit all key value pair
foreach ($array as $k => $v) {
// If there is nested array then
if (is_array($v)) {
// Call function for nested array
arrayToXml($v, $k, $_xml->addChild($k));
}
else {
// Simply add child element.
$_xml->addChild($k, $v);
}
}
return $_xml->asXML();
}
// Creating an array for demo
$my_array = array (
'name' => 'GFG',
'subject' => 'CS',
// Creating nested array.
'contact_info' => array (
'city' => 'Noida',
'state' => 'UP',
'email' => 'feedback@geeksforgeeks.org'
),
);
// Calling arrayToxml Function and printing the result
echo arrayToXml($my_array);
?>

Output:

<?xml version="1.0"?>
<root>
    <name> GFG </name>
    <subject> CS </subject>
    <contact_info >
        <city > Noida < /city >
        <state > UP < /state >
        <email > feedback@geeksforgeeks.org </email>
    <contact_info> 
<root>
 
Note: If the system generate error of type :PHP Fatal error: Uncaught Error: Class ‘SimpleXMLElement’ not found in /home/6bc5567266b35ae3e76d84307e5bdc78.php:24 then simply install php-xml, php-simplexml packages.