PHP str_replace() function - replace part of string from existing string


Syntax: str_replace("<replace from string>","<replace to string>","<source string>");
  
Replace the characters "I am" with "You are" from the sentence  "I am beautiful." 

<?php
echo str_replace("I","You","I am beautiful.");
?>


Output: You are beautiful.

1 comment :

Post a Comment

Leave A Comment...

Error Handling in PHP Simple Trick

Set error handler at beginning, write error handler function to track error . you can write one more function  to save error in error log file.  


//set error handler at beginning
set_error_handler("errorHandler ");

<?php


//error handler function
function errorHandler($errno, $errstr) 
{
echo "<b>Error Number :</b> $errno";
echo "<b>Error Message:</b> $errstr";
}



<Your error code here -start >
------
<Your error code here -End >


?>

No comments :

Post a Comment

Leave A Comment...

Read Current Directory Structure in PHP

Syntax: Read folder structure from root directory :  getcwd();
Output - C:\xampp\htdocs\<YOUR FOLDER DIRECTORY>
Description: <Your Folder Directory with root>

Syntax: Read folder structure from root directory : dirname(__FILE__);
Output - C:\xampp\htdocs\<YOUR FOLDER DIRECTORY>
Description: <Your Folder Directory with root>

Syntax: Red only current folder name :  basename(__DIR__)
& code is in “car-news” directory it will return only car-news
Output: car-news <You’re Folder Directory without root>


No comments :

Post a Comment

Leave A Comment...

Read Full URL in PHP


Here is the syntax, the Double quote is important below, this will show you your current URL


echo $FULL_URL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

To get HTTPS Version, change code with below syntax


$FULL_URL= (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";


No comments :

Post a Comment

Leave A Comment...