
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1060 Articles for PHP

978 Views
To find the length of the last word in the string, the PHP code is as follows −Example Live DemoOutputThe length of the last word is 3 The length of the last word is 6A PHP function named ‘last_word_len’ is defined, that takes a string as the parameter −function last_word_len($my_string) { // }The first occurrence of the space inside another string is found by using the ‘strrpos’ function. If that position is present, it is assigned to 0. Otherwise, it is incremented by 1 −$position = strrpos($my_string, ' '); if(!$position){ $position = 0; } else{ $position ... Read More

813 Views
To find the first word of a sentence, the PHP code is as follows −Example Live DemoOutputThe first word of the string is HiA string is defined at first −$my_string = 'Hi there, this is a sample statement';The ‘strtok’ function is an in-built function that is used to split a string into specified number of parts. Before the first space occurs, the string needs to be split. This split string’s first part is displayed as the output on the screen −echo "The first word of the string is ". strtok($my_string, " ");

980 Views
To find the number of characters in the string, the PHP code is as follows −Example Live DemoOutputThe number of characters in the string is 27Above, a string is defined that has more than the required spaces in between −$my_string = "Hi there, this is a sample ";Next, the length of the string is computed using the ‘strlen’ function. This length is assigned to a variable. This will give the characters present in the string including the spaces as well −$len = mb_strlen($my_string);The length computed is then printed on the screen −print_r("The number of characters in the string is "); echo ... Read More

4K+ Views
To sum the digits in a number, the PHP code is as follows −Example Live DemoOutputThe sum of digits is 11Above, a function named ‘sum_of_digits’ is defined, that takes an integer as a parameter.$my_num = "65";It first declares the variable sum as 0 and then iterates through the length of the declared integer, and adds the element at the ‘i’th place to the sum. Once the iteration is complete, this function returns the sum as output. Outside the function, the value is declared and the function is called by passing this element as the parameter −function sum_of_digits($my_num){ $sum = 0; ... Read More

8K+ Views
To check if a string has a special character, the PHP code is as follows;Example Live DemoOutputString has not been acceptedAbove, a function named ‘check_string’ is defined, that takes a string as its parameter −$my_string = 'This_is_$_sample!';Use regular expression to check if a string has a special character. If there is a special character, a specific message is printed. Outside the function, the string is defined, and the function is called by passing the string as parameter −function check_string($my_string){ $regex = preg_match('[@_!#$%^&*()?/|}{~:]', $my_string); if($regex) print("String has been accepted"); else print("String has not ... Read More

217 Views
To replace a word with a different symbol in a sentence, the PHP code is as follows −Example Live DemoOutputThe final replaced string is : is a simple onlyHere, a string is defined, and the string that needs to be replaced with a different string is placed inside an array and arranged to a variable −$my_str = "This is a sample only"; $search_str = array("sample", "This"); $replace_str = array("simple");The ‘str_replace’ function is used to replace the value with a different specified value. The result is printed on the screen −$result = str_replace($search_str, $replace_str, $my_str); print_r("The final replaced string is :"); print_r($result);Read More

617 Views
In this tutorial, we are going to learn how to swap two variables in different languages. Swapping means interchanging the values of two variables. Let's see an example.Inputa = 3 b = 5Outputa = 5 b = 3Let's see one by one.PythonWe can swap the variable with one line of code in Python. Let's see the code.Example Live Demo# initializing the variables a, b = 3, 5 # printing before swaping print("Before swapping:-", a, b) # swapping a, b = b, a # printing after swapping print("After swapping:-", a, b)OutputIf you run the above code, then you will get the following ... Read More

648 Views
To calculate the sum of square of first n natural numbers in PHP, the code is as follows −Example Live DemoOutputThe sum of square of first 5 natural numbers is 125A function named ‘sum_of_squares’ is defined that takes the limit up to which square of natural number needs to be found. In this function, a sum value is initialized to 0. A ‘for’ loop is run over the elements ranging from 1 to the limit. Every time, to the ‘sum’ variable, square of the iterated value is added. When the control reaches the end, the value of sum is returned as ... Read More

257 Views
To calculate the repeated subtraction of two numbers, the code is as follows −Example Live DemoOutputThe repeated subtraction results in 18A function named ‘repeated_sub’ is defined that checks to see if two values divide each other completely, and if this is true, it divides the numbers and gives the floor value of the quotient. Otherwise, it gives the floor value of quotient and the value computed by calling the ‘repeated_sub’ function on the second value, and the remainder when the values are divided.Outside the function, values are given to both the variables and the function is called by passing these values ... Read More

1K+ Views
To find the sum of odd numbers within a given range, the code is as follows −Example Live DemoOutputThe sum of odd natural numbers between the numbers 3 and 23 is 141.75A function named ‘odd_num_sum’ is defined that computes the sum of odd numbers within a specific range of numbers. The function ‘num_in_range’ gives the range of values between two numbers that are passed as parameters to this function. Outside both the function, the range values are defined and this function ‘num_in_range’ is called by passing the low and high values as parameters. Relevant output is displayed on the console.Read More