Very useful PHP code snippets for PHP Developer

This page contains a list of the very useful PHP code snippets for PHP Developers.
Submitted by Bhanu Sharma, on October 18, 2019 [Last updated : March 14, 2023]

A most useful list of PHP snippets that will help you to add value to your projects with minimal effort.

1) Getting current date & time in PHP

Here is the code to get the system's current date & time in PHP,

<?php
echo date('l jS \of F Y h:i:s A');
//output: Friday 18th of October 2019 12:39:53 PM
?>

2) print (get) file extension in PHP

Here, we're going to discuss the PHP pathinfo() function which is useful in getting information about a path. This function returns useful information about a file path including file name, extension, directory, and file base name. The below example explains the usage of pathinfo().

<?php
	$file_data = pathinfo('/path/to/dir/mynewfile.php');
	echo $file_data['basename'], "\n";
	echo $file_data['dirname'], "\n";
	echo $file_data['filename'], "\n";
	//returns the current file extension
	echo $file_data['extension'];
?>

Output

mynewfile.php
/path/to/dir
mynewfile
php

3) Include a Class from another file in PHP

To include a class in PHP, we can use any of the include/include_once or require/require_once methods. In this example, we will create the function in function.php file and then import it in index.php file.

Content of function.php:

<?php
class myNewClass {
	<!-- Function Goes Here -- >
}
?>

Content of Index.php:

<?php
require('function.php');
$vars = new myNewClass();
?>

Here, when a user visits index.php, upon initialization, function.php is called (due to require) and then treated as a part of index.php. Now, index.php can call functions from function.php as well.


4) Include php files when they're in different folders (directories)

Assume that we have the following directory structure:

Document Root

  • DirectoryA
    • file1.php
    • file2.php
    • file3.php
    • file4.php
    • file5.php
  • DirectoryB
    • index.php

Now, Assuming that we want to call file(1-5).php in index.php which is in DirectoryB, we can use the following approach:

Contents of index.php:

<?php
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file1.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file2.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file3.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file4.php');
include($_SERVER['DOCUMENT_ROOT'].'/../DirectoryA/file5.php');
?>

In the above example, $_SERVER gets the DOCUMENT_ROOT asset in the configuration for the given site and then performs actions relative to it, i.e.: /../ takes to the parent folder and then we add the relative path to file.

5) Import a CSS file in php without using HTML format

Here, we will learn how to import a CSS stylesheet into a PHP file without using the HTML link href.

<?php
	include('file.php');
	<style> //style tag
	// Include the style.css file
	Include('style.css'); 
	// Close the style tag
	</style> 
?>

6) Find similarity of two strings in PHP

There is a function similar_text() in PHP, that can be used to find the similarity of two strings, this function accepts three parameters: 1) string1, 2) string2 and 3) percent, percent will have the percentage of the similar text.

<?php
    $str1 = "Welcome @ IncludeHelp";
    $str2 = "Welcome @ IncludeHelp";
    $str3 = "Welcome atIncludeHelp";
    
    //Here, $percent will store the percentage of the similarity
    similar_text($str1, $str2, $percent);
    echo "similarity b/w str1 and str2 is: $percent \n";
    
    similar_text($str2, $str3, $percent);
    echo "similarity b/w str2 and str3 is: $percent \n";
?>

Output

similarity b/w str1 and str2 is: 100
similarity b/w str2 and str3 is: 90.47619047619

7) Generate random numbers in PHP

To generate random number in PHP, we can use rand() function – it returns a random number between 0 to getrandmax(). We can also specify the minimum and maximum values to generate the random numbers between them.

<?php
    //generating 10 random number
    echo "Random numbers are: \n";
    for($count=0; $count<10; $count++)
        echo (rand() . " ");

    //generating 10 random numbers between 50 to 99
    echo "\nRandom numbers b/w 50 to 99 are: \n";
    for($count=0; $count<10; $count++)
        echo (rand(50, 99) . " ");
?>

Output

Random numbers are:
477886177 1803134402 1833202175 1581092595 342280099 2092682811 
361945816 379084078 468756937 1242809286
Random numbers b/w 50 to 99 are:
98 74 50 72 77 61 75 50 75 96

8) Find remote IP address in PHP

<?php
        echo $_SERVER['REMOTE_ADDR'];
?>

Output

192.168.1.10

More PHP Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.