- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 1046 Articles for PHP
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
148 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
502 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
551 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
168 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
94 Views
To find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’, the code is as follows −Example Live DemoOutputThe sum of first 11 natural numbers divisible by 2 or 5 is 15A function named ‘sum_of_nums’ is defined that computes three values by checking if they can be divided by two specific values or not. Outside the function, the number and the two specific values are defined, and the function is called by passing these values as parameters. Relevant output is displayed on the console
444 Views
To find the numbers within a given array that are missing, the code is as follows −Example Live DemoOutputThe missing numbers in the array is 1 2 3 4 5A function named ‘missing_nums’ is defined that checks to see if a number is missing from an array of continuous numbers. It iterates through the array and checks to see the count and the current_num that is being iterated over. If two values can’t be found when 1 is added to the previous number, it is considered to be missing.Outside the function, the array is defined, its length is assigned to a ... Read More
161 Views
To find the first natural number whose factorial can be divided by a number ‘x’, the code is as follows −Example Live DemoOutputThe first natural number whose factorial can be divided by 16 is 4A function named ‘factorial_num’ computes factorial of a number and checks to see if it is divisible by 16, and if yes, returns that number as output. Outside the function, a number is defined and it is passed as parameter to the function. Relevant output is displayed on the console.
268 Views
To find if a number is present in a given sequence of numbers, the code is as follows −Example Live DemoOutputIs the number present in the sequence? Yes, it is present in the sequenceA function named ‘contains_in_sequence’ checks to see if two values are same, and if they are equal, the function returns true. If the difference between the two values multiplied by a third value is greater than 0 and the difference between the values divided by the third value gives a reminder as 0, the function returns true, otherwise, returns false. Values are assigned to the three variables and ... Read More