Kathmandu,Nepal
Kathmandu,Nepal

Display all list month of name using php

Example 1:

<?php

for($m=1; $m<=12; ++$m){
echo date('F', mktime(0, 0, 0, $m, 1)).'<br>';
}?>
Output:
January
February
March
April
May
June
July
August
September
October
November
December

Example 2:

<?php
$month_names = array("January","February","March","April","May","June","July","August","September","October","November","December");

foreach($month_names as $month){
echo $month."<br>";
}
?>
Output:
January 
February 
March 
April 
May 
June 
July 
August
September
October 
November 
December

Leave a Comment

Your email address will not be published. Required fields are marked *

Difference between print_r and var_dump in PHP

var_dump() and print_r() are both built-in functions in PHP that can be used to display the contents of a variable. However, they have some key differences:

1. print_r() Function: This function is a built-in function in PHP and is used to print information stored in a variable in a human-readable format. It is typically used for debugging purposes to see the contents of an array or object. It provides less detailed information about a variable compared to var_dump().

Example 1:

<?php
// Array with subjects
$array = array(
'0' => "PHP",
'1' => "Python",
'2' => "java",
);

// Display array values
print_r($array);
?>

Output:
Array
(
[0] => PHP
[1] => Python
[2] => java
)

Example 2:

<?php
$array = array ('x' => 'PHP', 'y' => 'Python', 'z' => array ('a', 'b', 'c'));
print_r ($array);
?>

Output:
Array
(
[x] => PHP
[y] => Python
[z] => Array
(
[0] => a
[1] => b
[2] => c
)
)

2.var_dump() Function: This function is a built-in function in PHP and is used to display the data type and value of a variable. It also shows the number of elements in an array and the properties of an object. It provides more detailed information about a variable compared to print_r() Function.
Example 1:

<?php
// Array with subjects
$array = array(
'0' => "PHP",
'1' => "Python",
'2' => "java",
);

// Display array values
print_r($array);
?>

Output:
array(3) {
[0]=>
string(3) "PHP"
[1]=>
string(6) "Python"
[2]=>
string(4) "java"
}

Example 2:

<?php
$x = array(1, 2,3, array("a", "b", "c","d"));
var_dump($x);
?>

Output:
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
array(4) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
[2]=>
string(1) "c"
[3]=>
string(1) "d"
}
}

Leave a Comment

Your email address will not be published. Required fields are marked *

Remove Duplicate Values from Array in PHP

To Remove Duplicate Values from Array in php,we use pre-define function array_unique that can help to remove duplicate values from Array.

Through array_unique() function, we can get only unique value from array.

<?php
$array = [11, 34, 56, 78, 34, 11, 23];
$newarray= array_unique($array );
print_r($newarray);
?>

Output:
Array
(
[0] => 11
[1] => 34
[2] => 56
[3] => 78
[6] => 23

)

Leave a Comment

Your email address will not be published. Required fields are marked *

Remove Empty Values from Array in PHP

We will do it using three different methods:

  1. array_filter() function.
  2. array_diff() functionon.
  3. unset() function.

Method 1: Using array_filter() function:

To remove all the empty values from array, we can use the array_filter() function. This function passes each value of the input array to the callback function. If the callback function returns true, then that value will be added to the returned array otherwise, it will be removed from the array.

In this example, we will removed the empty values from the array using the array_filter() function.

<?php
$array = array("HTML", "CSS", "JavaScript", "", "PHP", "");
$filtered_array = array_filter($array);
print_r($filtered_array);

Output:
Array (
[0] => HTML
[1] => CSS
[2] => JavaScript
[4] => PHP
)

Method 2: Using array_diff() function:

Apart from the array_filter() function, we can also remove the empty values from the array using the array_diff() function. This function is used to compare the values of two arrays or more arrays and returns a new array having the values of the first array, which are not present in other arrays.

In this example, we have removed the empty values from the array using array_diff() function and store the returned value in the new array.

<?php
$language = array("HTML", "CSS", "JavaScript", "", "PHP", "");
$filtered_array = array_diff($language,array(""));
print_r($filtered_array);

Output:
Array (
[0] => HTML
[1] => CSS
[2] => JavaScript
[4] => PHP
)

Method 3: Using unset() function:

In this example, we use to removed the empty values from the array using the unset() function.

<?php
$lang = array("HTML", "CSS", "JavaScript", "", "PHP", "", "jQuery");
foreach($lang as $key => $arr)
{
if($arr === '')
{
unset($lang[$key]);
}
}
print_r($lang);
?>
Output:
Array (
[0] => HTML
[1] => CSS
[2] => JavaScript
[4] => PHP
[6] => jQuery
)

Leave a Comment

Your email address will not be published. Required fields are marked *

Change date format in PHP

To convert the date-time format PHP provides strtotime() and date() function. We can change the date format from one format to another.

These are the built-in functions of PHP. The strtotime() first converts the date into the seconds, and then date() function is used to reconstruct the date in any format. Some examples are given below to convert the date format.

Change Date Format using the strtotime() funtion:

Example 1:
In this example, we use date 2021-08-15 in YYYY-MM-DD format, and we will convert this date to 15-08-2021

<?php
$orgDate = "2021-08-15";
$newDate = date("d-m-Y", strtotime($orgDate));
echo $newDate;
?>
Output:
15-08-2021

Example 2:
In this example, we use date 15-08-2021(DD-MM-YYY) format, and we will convert this date to 08-15-2021(MM/DD/YYYY) format.

<?php
$orgDate = "15-08-2021";
$newDate = date("m/d/Y", strtotime($orgDate));
echo $newDate;
?>
Output:
08/15/2021

Convert Date Format Using DateTime() function:

Example 1:
In this example, we use date 20-06-2021(DD-MM-YYYY) format, and we will convert this date to 2021-08-20(YYYY-MM-DD) format.

<?php
$orgDate = "20-06-2021";
$date = new DateTime($orgDate);
echo $date->format('Y-m-d');
?>

Output:
2021-06-20

Leave a Comment

Your email address will not be published. Required fields are marked *

Count the number of words in a string in PHP

To counts the number of words in a string,we use PHP function i.e. str_word_count().

The str_word_count() method is used to counts the number of words in a string.

<?php
$string = "Web design and development";
$words = str_word_count($string);
echo $words;
?>
Output:
4

Leave a Comment

Your email address will not be published. Required fields are marked *

Insert a dash after every nth character in PHP string

We will do it using two different methods:

  1. Using str_split() and implode() functions.
  2. Using wordwrap() function.

1. Using str_split() and implode() functions

The str_split() function use to split string into an array of equal length substrings.

<?php
$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$myarray = str_split($string, 4);
echo implode("-", $myarray);
?>

Output:
ABCD-EFGH-IJKL-MNOP-QRST-UVWX-YZ

Note: Join is an alias of Implode. If you want you can as well use join() in the place of implode().

2.Using wordwrap() function.

The wordwrap() function allows adding of character(s) to a string at regular intervals after a specific length

<?php
echo wordwrap('123456789123456789', 3, "-", true);
?>
Output:
123-456-789-123-456-789

Leave a Comment

Your email address will not be published. Required fields are marked *

Remove a certain character from a string in PHP

Method 1: Using str_replace() function

Example 1:

The following source code will remove all the occurrences of hyphens (-) from a phone number string.

<?php
$phone = "123-456-7890";
$phone = str_replace('-', '', $phone);
echo $phone;
?>
Output:
1234567890

Exaple 2:
To remove the occurrences of multiple characters or phrases in a string.

<?php
$string = "Apple, (Mango), 'Banana'";
$newstring = str_replace(array("(", ")", "'"), "", $string);
echo $newstring;
?>
Output:
Apple,Mango,Banana

Method 2: Using preg_replace() function

Method 1:

The preg_replace() function performs a regular expression search and replace, and returns a resulting string.

<?php
$phone = '123-456-7890';
$phone = preg_replace('/-/', '', $phone);
echo $phone;
?>

Output:
1234567890

Example 2:

To remove the occurrences of multiple characters in a string using this method, we include them in our regular expression.

<?php
$string = "Apple, (Mango), 'Banana]'";
$newstring = preg_replace("/[()']/", "", $string);
echo $newstring;
?>

Output:
Apple,Mango,Banana

Method 3: Using str_split() and implode()

The str_split() function splits a string into an array.

<?php
$phone = "123-456-7890";
$phoneArray = str_split($phone);
$phoneArray = array_filter($phoneArray, function ($char) {
return $char !== '-';
});
$phone = implode('', $phoneArray);
echo $phone;
?>

Output:
1234567890

Leave a Comment

Your email address will not be published. Required fields are marked *

Remove character from string in php

Example:1

The following source code will remove character from string:

<?php  
$res = preg_replace("/[^0-9.]/", "", "$123.099");
echo $res;
?>

Output:
123.099

Example:2

<?php
$res = preg_replace("/[^0-9]/", "", "Every 6 Months" );
echo $res;
?>
Output: 
6

Leave a Comment

Your email address will not be published. Required fields are marked *

Add days to current date in php

Add days to date

The following source code will add 5 days to date:

<?php
$date = "2020-10-15";
echo date('Y-m-d', strtotime($date. ' + 5 days'));
?>

Output:
2020-10-120

Add days to current date
The following source code will add 5 days to date:

<?php echo date('Y-m-d', strtotime(' + 5 days')); ?>

Leave a Comment

Your email address will not be published. Required fields are marked *

×