Web Development & WordPress

Reverse String in various ways

CODE:

<?php

$sentence = "My name is Jon";
echo "<br>Main sentence is:<br>";
echo $sentence;
$rev1= strrev($sentence);
echo "<br>First Reversal:<br>";
echo $rev1;
echo "<br>2nd Reversal system: <br>";
$rev2= "";
for($i=1;$i<=strlen($sentence);$i++){
 
$rev2.=$sentence[$i * -1] ;
}

 
echo  $rev2 ;

$rev3="";
echo "<br>3nd Reversal system: <br>";

for($i=strlen($sentence);$i>=0;$i--){
$rev3.= $sentence[$i];
}
echo $rev3;
?>
Continue reading “Reverse String in various ways”
Web Development & WordPress

String in PHP heredoc and nowdoc

CODE:

String in PHP heredoc and nowdoc
<?php

$beVerb = "is";
$sentence1 = 'My name $beVerb Jim';
echo "Sentence 1 in single quotation";
echo "<br>";
echo $sentence1;
echo "<br>";
$sentence2 = "My name $beverb Jim";
echo "Sentence 1 in single quotation<br>";
echo $sentence2;
echo "<br>PHP HERE DOC EXAMPLE<br>";
$str = <<<tom
the beverb i used $beVerb 
another test
'this is multiple test'
and the be verb is $beVerb
another "test"
tom;

echo $str;

echo "<br>PHP NOW DOC EXAMPLE<br>";
$str = <<<'tom'
the beverb i used $beVerb 
another test
'this is multiple test'
and the be verb is $beVerb
another "test"
tom;

echo $str;

?>
Continue reading “String in PHP heredoc and nowdoc”
Web Development & WordPress

Array Splice to cut data from main array and insert new data : PHP

CODE:

<?php
$name=array("Bobby", "Robby","James","Tobby","Inaya","Jack");
echo "Main Array at the begining <br>";
print_r($name);
echo "<br>New array after Splice<br>";
$newEntry=array("Robin","Tony");
$name1 = array_splice($name,2,-1,$newEntry);
print_r($name1);
echo "<br>Main array after Splice including new entry<br>";
print_r($name);
?>
Continue reading “Array Splice to cut data from main array and insert new data : PHP”
Web Development & WordPress

Array slice to extract data in php

Code:

<?php
$name= array("Tom", "Jon","Rowan","Jim","Harry","Rasha");
echo "Main Array";
echo "<br>";
print_r($name);
echo "<br>";
echo "slice upto 3";
echo "<br>";
$name1=array_slice($name,3);

print_r($name1);
echo "<br/>";

echo "Keeping Key same: slice 3,2";
$name2=array_slice($name,3,2,true);
print_r($name2);
echo "<br/>";
echo "slice 3,-1<br>";
$name3= array_slice($name,3,-1);
print_r($name3);

echo "<br>slice 3,-2<br>";
$name3= array_slice($name,3,-2);
print_r($name3);
echo "<br>slice -3,-2<br>";
$name3= array_slice($name,-3,-2);
print_r($name3);

echo "<br>slice -3,-1<br>";
$name3= array_slice($name,-3,-1);
print_r($name3);
echo "<br>slice -3,1<br>";
$name3= array_slice($name,-3,2);
print_r($name3);
?>
Continue reading “Array slice to extract data in php”
Web Development & WordPress

JSON to php object and array

Code:

<?php
class StudentInfo{
    public $name;
    public $age;
    private $mobileNum;
    public  function setMobileNum($mobileNum){
        $this->mobileNum=$mobileNum;
    }
    public function getMobileNum(){
        return $this->mobileNum; 
    }
}
$student = new StudentInfo();
$student -> name ="Tommy Jones";
$student -> age =45;
print_r($student);
$student->setMobileNum('11111111');
echo "<br>";
print_r($student);
$jsonEncoded=json_encode($student);
echo "<br>";
print_r($jsonEncoded);
$jsonDecoded=json_decode($jsonEncoded);
echo "<br>";
print_r($jsonDecoded);
$jsonDecodedArray=json_decode($jsonEncoded,true);
echo "<br>";
print_r($jsonDecodedArray);
?>
Continue reading “JSON to php object and array”
Web Development & WordPress

PHP Object to JSON

Only Public ones will be converted

Code:

<?php 
class studentInfo{
    public $name;
    public $age;
    private $mobileNum;
public function setMobileNum($mobileNum){
    $this->mobileNum=$mobileNum;
}
public function getMobileNum(){
    return $this->mobileNum;
}


}
$student = new studentInfo();
$student->name= "Tom hanks";
$student->age = 45;
 $student->setMobileNum('11111111');
//echo $student->getMobileNum();
echo json_encode($student);
?>
Continue reading “PHP Object to JSON”
Web Development & WordPress

isset(), empty() and is_numeric() in PHP

CODE:

<?php

$studentNum=0;
if(isset($studentNum)){
echo "value is set ";
}else{
    echo "value not set";
}
echo "<br>";
if(empty($studentNum)){
    echo "value is empty";
}else{
    echo "value not empty";
}
echo "<br>";
if(isset($studentNum) && (is_numeric($studentNum) || $studentNum !='')){
    echo "there is value and not empty";
}else{
    echo " not set or empty";
}
?>

OUTPUT:

value is set
value is empty
there is value and not empty