Trending November 2023 # How Does The Filter_Var Function Work In Php? # Suggested December 2023 # Top 11 Popular

You are reading the article How Does The Filter_Var Function Work In Php? updated in November 2023 on the website Minhminhbmm.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested December 2023 How Does The Filter_Var Function Work In Php?

Introduction to PHP filter_var

Php filter_var() is a function that is used to filter a given variable with a specified filter. To sanitize and validate the data such as email_id, IP address, etc., in Php, the filter_var() function is used (which contains the data). Validation in the text means whether the entered data is in the correct format or not. For example, in an email id of the person, whether the ‘@’ sign is present or not. In a phone number field, all the numbers or digits should be present. Sanitization means to sanitize the data entered or remove the illegal or unnecessary characters from it to prevent any future issues. For example, removing unnecessary symbols and characters from user email.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax:

filter_var(variable, filtername, options)

where,

variable: This parameter stands for the variable field, the variable which needs to be filtered. It is the mandatory field.

filtername: This parameter stands for the name of the filter which the user wants to use. It is an optional parameter. If not specified, FILTER_DEFAULT is used, which means that not filtering would be done to the given variable.

options: This parameter is optional. It specifies the options/ flags to be used. It is basically an associative array of bitwise disjunctions of flags or options. If this parameter is used in the filter_var() function, a flag must be provided in the ‘flags’ field, and a callable type must be passed for the callback function. After accepting all the parameters, the filtered and sanitized variable is returned.

Return Value: The above function returns the filtered value or false if the data/ variable does not get filtered.

How does the filter_var function work in Php?

In PHP, the filter_var() method accepts the above-explained various parameters and returns the validated/ sanitized data. Validation means checking the format of the data as specified by the programmer, and Sanitization means removing the unnecessary characters from the data to return the data as required by the programmer.

Examples of PHP filter_var

Let us understand the working of the filter_var() function in Php along with the examples:

Example #1

Validating an Integer value using filter_var() function:

Code:

<?php $value = 789787; if (filter_var($value, FILTER_VALIDATE_INT)) { echo(“Congratulations!!! $value is a valid integer value”); } else { echo(“Sorry!! $value is not a valid integer value”); }

Output:

Explanation:

In the above code, the Integer value to be validated is stored in the variable ‘value’ and is then passed in the filter_var() method along with the ‘FILTER_VALIDATE_INT’ filter name to validate it. Finally, conditional operators if and else are applied to check the condition, and the respective output is printed on the console using the ‘echo.’

Example #2

Validating the IP address of the computer device using the filter_var() function

Code:

<?php $ip = ‘180.0.0’; if (filter_var($ip, FILTER_VALIDATE_IP)){ echo(“Congratulations!! $ip is a valid IP address, passed by the you”); } else { echo(“Sorry $ip is an incorrect IP address”); }

Output:

In the above code, the IP address of the computer or any other network device is validated using the filter_var() method. The ip address that is to be validated is stored in the variable ‘ip.’ Since the IP address has its specific format ‘x.y.z.w,’ it is validated using the ‘FILTER_VALIDATE_IP’ in the filter_var() function. Finally, the ip address passed is validated, and the respective output is printed on the console using ‘echo.’

Example #3

Sanitizing and Validating the URL address using the filter_var() function

Code: 

<?php $check_url = filter_var($check_url, FILTER_SANITIZE_URL); if(!filter_var($check_url, FILTER_VALIDATE_URL) == false) { echo(“Congratulations!!! $check_url is the correct URL”); } else { echo(“Sorry!! $check_url is an invalid URL”); }

Output:

Explanation:

In the above code, the URL address, which has a specific format, is sanitized first and then validated using the filter_var() method. The URL to be checked is stored in the variable ‘check_url.’ To sanitize the url, ‘FILTER_SANITIZE_URL’ is passed as a filter name along with the url. Once sanitized, url is then validated using the ‘FILTER_VALIDATE_URL’ filter name along with the url, and the respective output on validation is printed on the console using ‘echo.’

Example #4

Validating the email address of the user using the filter_var() function

Code:

<?php $email_check = “[email protected]”; if (filter_var($email_check, FILTER_VALIDATE_EMAIL)) { echo(“Congratulations!! $email_check is a valid email address”); } else { echo(“Sorry!! You have entered an incorrect email address”); }

Explanation:

In the above example, the email address which is to be checked is stored in the variable ‘email_check.’ It is validated using the filter_var() function in Php, bypassing the email variable and the respective filter name (FILTER_VALIDATE_EMAIL). Since the passed email is invalid, so the response is printed on the console using the ‘echo.’

Example #5

Code:

<?php $value = 465675; { echo “Integer $value is within the specified range”; } else { echo “Sorry!! Integer $value is not in the range provided by you”; }

Output:

Explanation:

In the above example, the Integer value is to be validated for the given range, i.e., 10 to 400 is tested. Then, in the filter_var() function, the value to be tested is passed along with the filter name (FILTER_VALIDATE_INT) and 1 optional parameter, i.e., ‘options’ having the array with the minimum and maximum range specified. Finally, the variable is validated, and accordingly, the response is printed on the console using the ‘echo.’

Conclusion

The above description clearly explains what is filter_var() functions in Php and how it works to validate and sanitize the variable passed in it. It is one of the important functions that programmers commonly use to filter the data to prevent a security breach. However, this function facilitates the use of different filters by passing the different parameters according to the specific requirements, so the programmer needs to understand it deeply before using it in the program.

Recommended Articles

This is a guide to PHP filter_var. Here we discuss the introduction, syntax, and working of the filter_var function in Php along with different examples and code implementation. You may also have a look at the following articles to learn more –

You're reading How Does The Filter_Var Function Work In Php?

How Addslashes() Function Works In Php?

Introduction to PHP addslashes() function

addslashes() function is an inbuild and pre-defined function in PHP which is used for returning a string containing backlashes and is prefixed before all predefined characters present within the string. PHP addslashes() function has a special characteristic that it does not consider any parameters or arguments to be passed from the function. The predefined characters behave differently in this function. addslashes function somewhat differently in comparison to the addcslashes() function because this function accepts specified characters that need to be added before the slashes but is not important for addslashes() function as it doesn’t allow any parameter to pass and appends the slash before the specified character.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Syntax

addslashes($string)

addslashes(): The addslashes() function allows only one parameter or argument to be passed from within the function followed by the $ symbol of string.

$string : This is the argument that will be passed to the addslashes function as an input to the function. it specifies a special structure of the attribute designating the string.

How addslashes() function works in PHP?

addslashes is a special inbuild function in the PHP defined string references and string packages which generates a quote for the quote string with slashes. It has a return type which returns the string with a special format in a way that the characters within the string will be prefixed following a string of characters:

Single quote: It is represented by a symbol (‘)

Double quote: It is represented by a symbol (” “)

Backslash: It is represented by a symbol ( )

Null: It is represented by the (NUL byte)

Use cases will vary with the string of characters in a way that the characters that will be prefixed before the string with these characters will be escaped set of characters.

This function has been added and came into its usage after PHP version 5.4.0 and above, otherwise, the default value was being considered as magic_quotes_gpc on all types of GET, POST, and cookies being used on the string as mentioned for addslashes() function.

Sometimes this function behaves abnormally once incorporated with the database queries. Therefore, it depends on the type of requirement and versions to check and cope up with the addslashes function once it is in use with PHP. Since this makes it ambiguous to make it work with databases there are times or scenario that may arise for making it sync with Database injection and its related queries at that time it is very much needed to make database-specific escaped functions or prepared statements for its use.

Moreover, many substrings which act or are a part of the addslashes function which includes following function as its companion:

stripcslashes(): It is a function as part of addslashes which is used to generate some un-quoted string but are quoted with some addcslashes.

stripslashes(): It is also a companion of addslashes function which unquote any of the generated quoted string.

addcslashes(): It is another part of addslashes function which works totally complimentary with the addslashes function in a way that it quotes string with inbuild function including a C style pattern for it.

htmlspecialchars(): This function works in a way that a special character gets converted into HTML entities.

Quotemeta(): This function works for characters within characters which are called meta characters type.

get_magic_quotes_gpc(): This function also behaves in a similar fashion with a very less difference of present settings in configuration for setting of magic_quotes_gpc.

Examples of PHP addslashes() Function

Below are the different examples :

Example #1

This program represents the addslashes function for the representation of the string with an output of the generated string with escaped sequences of characters.

Code:

<?php $str = addslashes(‘hope you are doing good with educba!’); echo($str);

Output:

Example #2

This program represents the addslashes function for the representation of the string with an output of the generated string with escaped sequences of characters but supporting the ambiguous behavior of the database injection.

Code:

<?php $str = “Which car is Volkswaon?”; echo addslashes($str) . “  Safe for database injection.”;

Example #3

This program represents the addcslashes() function for the representation of the string with an output of the generated string with escaped sequences of characters by adding a backslash in front of the letter W which behaves completely opposite of addslashes() function.

Code:

<?php $str = addcslashes(“Welcome Educba!”,”W”); echo($str);

Output:

Example #4

This program represents the addcslashes() function for the representation of the string with an output of the generated string with escaped sequences of characters by adding a backslash in front of the letter educba for adding backslashes which behave completely opposite of addslashes() function.

Code:

<?php $str = “Thanks for revisiting the educba portal!”;

Output:

Example #5

Code:

<?php $str = “Thanks for revisiting to our portal of educba!”; echo addcslashes($str,’a..g’);

Output:

Example #6

This program represents the HTML special characters with the elements to translate the element’s value into one value using string.

Code:

<?php $str = “There are some good person.”; echo htmlspecialchars($str);

Output:

Conclusion

The addslashes function is used to represent and traverse the string by prefixing the backslash string in front of the entire special character string and is used to quote the necessary and important string to be used simultaneously.

Recommended Articles

This is a guide to PHP addslashes(). Here we discuss how addslashes() function works in PHP along with multiple examples and its code implementation. You may also have a look at the following articles to learn more –

How Does Isnan() Function Works In Javascript?

Introduction to isNaN() JavaScript

In this article, we will learn about isNaN() JavaScript. We will try to split the function isNaN() word by word and analyze the meaning of the function. is and NaN is both 2 separate words. NaN abbreviation is Not a Number. Now if we include any helping verb in front of any word, give a question right. Here also, isNaN means checks given value is a Number or Not. isNaN() checks whether the value passed to it is true or false.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How does the isNaN() function work in JavaScript?

isNaN() function always checks whether the value is a Number or Not a Number.

isNaN() returns the Boolean value as output.

Returned Boolean values are true or false.

If the given value is a string, it returns true; if the given value is a number, it returns false.

isNaN(value);

value: Pass the required value to check whether it is a number or not.

Example: There is a situation if we want to add or subtract numbers. Let’s suppose the numbers we are getting are from 3rd party clients. Are we directly add or subtract those values? No, because We don’t know what those values are, whether numbers or strings. So, we first check whether it is a number or not by using the isNaN() function. If the number is in string form, we simply par the number. Later we will add or subtract.

Examples to Implement in isNaN() JavaScript

Below is the example of implementing in isNaN() JavaScript:

Example #1

Checking whether passing Strings are numbers or not

Code:

function checkStringsNumberOrNot() { var a=”Amardeep”; var b=”123″; var c=’25/12/2023′; var d=”123Param”; var e=”Hi989″; var f=”*&^%”; var g=123+”Hello”; } checkStringsNumberOrNot();

Output:

Explanation of the above code: Amardeep is not a number, so the function returns true. 123 is a number, so the function returns false. 25/12/2023 is not a number but a date, so the function returns true. 123Param is not a number, so the function returns true. Hi989 is not a number, so the function returns true. 8&^% is not a number, so the function returns true. 123Hello is not a number, so the function returns true. (123+” String”=String so becomes 123Hello).

Example #2

Checking whether passing integers are numbers or not

Code:

function checkIntegersNumberOrNot() { var a=”989″; var b=23; var c=-25; var d=-5.21; var e=’+28.67F’; var f=”87.23L”; var g=’0′; } checkIntegersNumberOrNot();

Explanation of the above code: 989 is a number, so the function returns false. 23 is a number, so the function returns false. -25 is a number, so the function returns false. -5.21 is a number, so the function returns false. +28.67F is not a number, so the function returns true. 23L is not a number, so the function returns true. 0 is a number, so the function returns

Note: Whereas in Java, suffixes F and L indicate float and long numbers, respectively, JavaScript doesn’t.

Example #3

Checking whether passing predefined JavaScript values are numbers or not

Code:

function checkPredefinedValuesNumberOrNot() { var a=”true”; var b=”false”; var c=”undefined”; var d=”null”; var e=0/0; var f=NaN; var g=”NaN”; } checkPredefinedValuesNumberOrNot();

Output:

Explanation of the above code: true is not a number, so the function returns true. false is not a number, so the function returns true. Undefined is not a number, so the function returns true. null is not a number, so the function returns true. NaN(0/0) is not a number, so the function returns true. NaN without quotes is not a number, so the function returns true. NaN with quotes is not a number, so the function returns true.

Example #4

Its checks passed value is NaN, and its type is number. It is an updated version of the isNaN() direct-using function. It also returns true or false based on the value provided to it.

Code:

function checkValuesNumberOrNot() { var a=10; var b=”false”; var c=0/0; var d=-21.7; var e=Number.NaN } checkValuesNumberOrNot();

Output:

Explanation of the above code: 10 is a number, so the function returns false. false is a number, so the function returns false. NaN (0/0) is not a number, so the function returns true. -21.7 is a number, so the function returns false. NaN (Number.NaN) is not a number, so the function returns true.

Note:

Number.isNaN() returns the false and true output as false only because it considers false as 0 and true as 1 here.

Function isNaN continuously checks whether the value inside the function is a number or not, whether in single quotes, double quotes, or no quotes.

Conclusion

isNaN() function is used to figure out whether a given value is a number or not. If the given value is a number, then return false otherwise, return true.

Recommended Articles

This is a guide to isNaN() JavaScript. Here we discuss an introduction, how isNaN() JavaScript works, and examples to implement. You can also go through our other related articles to learn more –

Learn How Does The Numpy.clip() Function Works?

Introduction to Numpy.clip() in Python

Web development, programming languages, Software testing & others

In simpler terms for an interval specified (for instance : [0, 1]) the values that are greater than 1 shall deem to become one and the ones smaller than zero shall deem to become zero. In comparison to using the function min() and max() and checking their comparatives by maximum(), the clip() serves a much quicker and more comprehensive solution when compared to running the while loop.

Syntax and Parameters for Numpy clip()

Following syntax is used structurally to construct code in python language:

numpy.clip (arr, a _min, a_max , out = None)

Following are the parameters that are used in the syntax for numpy.clip() function in Python:

Parameters Description

array ( here arr) (alternatively, can be specified within the code itself)

(Scalar value, keyword: “arraylike” or keyword: “None”) If NONE is specified then the lowest element of the array would be considered to be the smallest element in the array entered. It has to be noted that the parameter NONE should not be specified for both a_min and a_max.  If either one of the parameters is kept as ARRAYLIKE it results in 3 different arrays being broadcast.

(Scalar value, keyword: “arraylike” or keyword: “None”) The highest value to be put for the array limit is the upper extent with which the array elements have been checked if they are larger than the lower limit.

Return Value when running through Numpy.clip()

This Numpy.clip() function returns a two-dimensional array that has been specialized from the string of elements that have been presented in the array.

numpy.clip ( arr,a_ min, a _ max, out  = None )

Returns: Here the lower values are replaced by a_min values and higher limits are replaced by a_max

Example of Numpy.clip()

Code:

# To demonstrate the usage of the Numpy clip () function in python language # calling the Numpy by importing it to perform the clip function import numpy as N1 Ar_array = [10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 ] print ("Please enter the elements for the array :- ", Ar_array ) Output_array = N1.clip(Ar_array, a_min = 20 , a_max = 60 ) print ("The new clipped array will be : " ) print Output_array

The output of the above-given code is as follows:

How does the Numpy.clip() function work?

It is found in a lot of data concerning issues and algorithmic functionalities (for instance the Proximal Policy Optimization or PPO used in algorithms of reinforcement learning) where there is a need to limit the elements under an upper or lower value or both.

The numpy clip serves the purpose of delivering a pre-built functionality of limiting the values.

The following diagram pictographically displays how the clip function actually works and gives an insight into its mechanism of limiting

Fig: The image here displays how the default values using index numbers are identified and put under the clipped limit values

The system first analyses the values present in the array entered by the user

It then checks the limits for both the upper value and lower value

It then compares with each element if it does not confer to the limits and checks for their index with respect to the initial array entered

It changes the defaulting dex number to the upper limits and lowers limits specified.

Finally, it changes the values with the replaced limited values and makes a new array that suffices the need specified for the function to be performed by the user

Conclusion

The numpy clip function serves as one argument/liner solution to give clipped arguments for arrays which are frequently required by various algorithms which in a wat reduce the computational time needed to run code. It also decreases the verbosity of the code making it better for large data analysis.

Recommended Articles

This is a guide to Numpy.clip(). Here we discuss introduction, syntax, parameters and working of Numpy.clip() function along with the examples. You may also have a look at the following articles to learn more –

How Does Redirect Work In Javascript?

Introduction to JavaScript Redirect

JavaScript redirect is the process of sending requests form one page to another page through accessing the corresponding URL (Unified Resource Locator). Redirecting URL is also used for sending the user from one URL to another URL. location is the function used in JavaScript to redirect at the specific URL.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Real Time Scenario:

Advantage:

We can add more information within a single page by this URL redirect.

Pre-requisites:

Basic HTML

Basic JavaScript

How does Redirect Work in JavaScript?

JavaScript redirect is working based on different types of redirect methods. Each redirect has its own specification.

Syntax:

location = "URL";

Description: It will set the new location for the current window.

location.href = "URL";

Description: It will set the new href for the current window.

location.assign("URL");

Description: It will assign the new URL to the current window.

location.replace("URL");

Description: It will replace the current window location with a new location.

location = "URL";

Description: It will just set the current window location itself.

location = "URL";

Description: It will set the topmost window location with a current window location.

Note: The difference between href and replace method is replace() method removes the URL of the current document from the document history, means it is not possible to use the “back” button for navigating to the original document.

Examples of JavaScript Redirect

Given below are the examples mentioned:

Example #1

Window location URL.

h1 { text-align: center; color: green; } p { font-size: 28px; border: solid 3px blue; color: maroon; } page to other page through accessing the corresponding URL (Unified Resource Locator). Redirecting URL is also used for sending the user from one URL to another URL. Window.location is the function used in website, while I am reading, I have some doubts with specific topics. Instead of providing everything there itself, we can simply summarize the topic and in between doubtable points can be provide with URL window.location = url;

Output:

After few milliseconds (1sec) moved to URL:

Example #2

Window Location Redirect with Time Limit.

Code:

h1 { text-align: center; color: brown; } p { font-size: 28px; border: solid 3px red; color: green; } input { text-align: center; color: navy; } button { font-size: 22px; font-weight: bold; color: white; background: lightblue; } page to other page through accessing the corresponding URL (Unified Resource Locator). Redirecting URL is also used for sending the user from one URL to another URL. Window.location is the function used in website, while I am reading, I have some doubts with specific topics. Instead of providing everything there itself, we can simply summarize the topic and in between doubtable points can be provide with URL Type URL :<input type=”text” name=”url” function getMyRedirectURL() { var url = document.getElementById(“urlID”).value; document.write(“It will redirect within 3 seconds…..please wait…”);//it will redirect after 3 seconds setTimeout(function() { window.location = url; }, 3000); }

Output:

Example #3

Replace Function URL.

Code:

h1 { text-align: center; color: navy; } p { font-size: 28px; border: double 2px teal; color: lime; } input { text-align: center; color: fuchsia; } .button { text-align: center; } button { font-size: 22px; font-weight: bold; color: white; background: red; } page to other page through accessing the corresponding URL (Unified Resource Locator). Redirecting URL is also used for sending the user from one URL to another URL. Window.location is the function used in website, while I am reading, I have some doubts with specific topics. Instead of providing everything there itself, we can simply summarize the topic and in between doubtable points can be provide with URL function getMyReplaceFun() { }

Output:

Recommended Articles

This is a guide to JavaScript Redirect. Here we discuss the introduction to JavaScript Redirect, working, and examples respectively. You may also have a look at the following articles to learn more –

How Does Sprintf Work In Python?

Definition of sprintf in Python

The sprintf is a function to display the output of the given input using a python programming language.

The sprintf is a print function to shows the output of the format strings in the python language.

It is a coding element to assigns the “f get” method and displays string format output.

The sprintf is a print element to contain string – buffer data of the application and display in the string format using python technology.

The sprintf is a function similar to print, vprint for display buffer output hustle-free.

The python programming language is using the sprintf function to declare argument and constructor elements.

It is an output function to displays all data types like string and array elements.

Syntax:

Start Your Free Software Development Course

The sprintf python works with different data types, lengths of the data, and width.

It has used the percentage sign (%) before the type of the data.

The basic syntax of a sprintf shows below.

% [FLAG WIDTH. (DOT) PRECISION] TYPE

The sprintf python is using the “print” keyword to display output.

print("% [flag width . (dot) precision] type" % (value or object))

The sprintf is used precision and type depends on the data type of the variable. This syntax helps to assign a signed decimal number. The length value of a decimal is 2.

%2d

This syntax helps to assign a binary number. The length value of binary is 4.

%4b

This syntax helps to assign a floating number. The length value of a decimal is 2.1.

%2.1f or %2.1F

This syntax helps to assign ASCII values.

%c

This syntax helps to assign unsigned decimal values.

%u

This syntax helps to assign an octal number.

%o

This syntax helps to assign a hexadecimal number.

%x OR %X

This syntax helps to assign scientific notation of a lowercase.

%e

This syntax helps to assign scientific notation of an uppercase.

%E

This syntax helps to return the type of data format.

%%type How does sprintf work in Python?

Download python software from the respective website. Create a page with the dot (.) py extension. The file name is the “function.py” to write a python program. Create a variable with initializing the required data type value.

Varble_name = 34

Use the print keyword for the string format of the sprint python.

print (write sprint format)

Use percentage sign to return value.

print (" text : %d ")

Display length of the variable or value before data type.

print (" text : %2d ")

Display the type of the variable value in the format helping of the sprint. Use parameter of the percentage sign to end of the sprintf function end. Add variable to interconnect return function and application data.

print (" text : %2d " % (Varble_name))

Use direct value in sprint function to display decimal value.

print("decimal number : %2d " % (7))

Use float type with sprintf formatted value in the return function.

print("Float number : %5.2f" % ( 23.11))

Combine the working procedure of the sprintf python to better understanding.

x = 34 print ("decimal number : %2d " % (x)) print ("decimal number : %2d " % (7)) print ("Float number : %5.2f" % ( 23.11)) Examples Example #1 – Basic

Code:

e_var = 34 print ("decimal number: %2d " % (e_var)) print ("decimal number: %2d " % (7)) print ("Float number: %5.2f" % ( 23.11)) print ("Float number: %5.4f" % (e_var)) print ("Octal number: %5o" % (e_var)) print ("Octal number: %3o" % (42))

Output:

Example #2 – With different types

Code:

e_var = 341234673 print ("decimal number: %d " % (e_var)) print ("Float number: %f" % (e_var)) print ("Float number: %F" % (e_var)) print ("unsigned decimal number: %u" % (e_var)) print ("Octal number: %o" % (e_var)) print ("first string value: %s" % (e_var)) print ("second string value: %s" % ("string data")) print ("first hexadecimal value: %x" % (e_var)) print ("second hexadecimal value: %X" % (e_var)) print ("ASCII value: %c" % ("A")) print ("lowercase scientific notation: %e" % (e_var)) print ("uppercase scientific notation: %E" % (e_var)) print ("first value: %g" % (e_var)) print ("second value: %G" % (e_var))

Output:

Example #3 – With positive and negative value e_var = 341234673 f_var = -341234673 print ("decimal number: %d " % (e_var)) print ("decimal number: %d n " % (f_var)) print ("unsigned decimal number: %u" % (e_var)) print ("unsigned decimal number: %u n" % (f_var)) print ("Octal number: %o" % (e_var)) print ("Octal number: %o n" % (f_var)) print ("first hexadecimal value: %x " % (e_var)) print ("first hexadecimal value: %x n" % (f_var)) print ("second hexadecimal value: %X " % (e_var)) print ("second hexadecimal value: %X n" % (f_var)) print ("lowercase scientific notation: %e" % (e_var)) print ("lowercase scientific notation: %e n" % (f_var)) print ("uppercase scientific notation: %E" % (e_var)) print ("uppercase scientific notation: %E n" % (f_var)) print ("first value: %g" % (e_var)) print ("second value: %G" % (f_var))

Output:

Example #4 – With different length

Code:

e_var = 341234673 f_var = -341234673 print ("decimal number: %2d " % (e_var)) print ("unsigned decimal number: %1u n" % (f_var)) print ("Octal number: %2o n" % (e_var)) print ("first hexadecimal value: %1x " % (e_var)) print ("second hexadecimal value: %5X n" % (e_var)) print ("lowercase scientific notation: %2e" % (e_var)) print ("uppercase scientific notation: %1E n" % (f_var)) print ("Float value: %2.1f" % (e_var)) print ("Float value: %1.2f n" % (f_var)) print ("Octal value: %2o" % (e_var))

Output:

Conclusion

It is easy to return data in any format per application requirement.

It helps to create web applications attractive, understandable, and user-friendly.

Recommended Articles

We hope that this EDUCBA information on “sprintf Python” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

Update the detailed information about How Does The Filter_Var Function Work In Php? on the Minhminhbmm.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!