Solved

Php sprintf('%u', $x) - explanation

Posted on 2013-01-11
3
437 Views
Last Modified: 2013-01-11
Hello experts.
I'm using coldfusion server language and need help to understand this function:
// IP ADDRESSES ARE UNSIGNED AND MAY RETURN NEGATIVE VALUES
$ip_number = sprintf('%u', $x);

(4 step - second code - line 39 here: http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_3437-IP-Address-to-Country-in-PHP.html)

Any help?
0
Comment
Question by:Panos
3 Comments
 
LVL 30

Assisted Solution

by:Brad Howe
Brad Howe earned 150 total points
ID: 38766506
Just formating hte variable ip_number to an integer.

http://php.net/manual/en/function.sprintf.php

eg: string sprintf ( string $format [, mixed $args [, mixed $... ]] )

%u, u - the argument is treated as an integer, and presented as an unsigned decimal number.
$X is a variable placeholder within the script.

// IF NO IP ADDRESS IS GIVEN, USE THE REMOTE ADDRESS OF THE CLIENT
$ip = $_SERVER["REMOTE_ADDR"];
// CONVERT TO BINARY - SEE http://php.net/manual/en/function.ip2long.php
$x = ip2long($ip);
// IP ADDRESSES ARE UNSIGNED AND MAY RETURN NEGATIVE VALUES
$ip_number = sprintf('%u', $x);


Cheers,
Hades666
0
 
LVL 10

Accepted Solution

by:
d4durvesh earned 350 total points
ID: 38766600
Hi panosms,

As you may know we use 'echo' keyword to display output on the page,but sometimes it becomes tedious and bit of dirty.Lets see an example,
$name = "Gates";
$age = 55;
$work = "Microsoft"
echo "My name is $name and my age is $age,I work in $work";

Open in new window

As you see the string becomes to much of lengthy and unreadable.
So to make life simpler there is one way,

Use function printf() [hold on I will come to sprintf() ]
 
printf() functions works in following way
It takes two arguments,string to be displayed and variables to be inserted in it,
Example,
$name = "Gates";
$age = 55;
$work = "Microsoft"

printf("My name is %s and my age is %d,I work in %s",$name,$age,$work);

Output: My name is Gates and my age is 55,I work in Microsoft

Open in new window

That means it adds string in place of %s i.e., variable $name,number in place of %d.

But suppose we want to store this output we will try to do like this,
$name = "Gates";
$age = 55;
$work = "Microsoft"

$output = printf("My name is %s and my age is %d,I work in %s",$name,$age,$work);
echo $output;

Open in new window

But hold on this is not going to work the output we will get is
My name is Gates and my age is 55,I work in Microsoft45
The 45 is number of characters are there in string.
So to avoid this we do slightly other way,
we use sprintf() function.
The only difference between sprintf() and printf() is that sprintf() stores the value in variable.

$name = "Gates";
$age = 55;
$work = "Microsoft"

$output = sprintf("My name is %s and my age is %d,I work in %s",$name,$age,$work);
echo $output;

Open in new window

Here are some things you need to remeber,
%s reprensent string,
%d represent signed decimal
%u represent integer and presented as an unsigned decimal number.

There are more arguments you can pass on just check this,
http://php.net/sprintf

Also let me know if you still having doubts,I will be glad to explain you in more detail.

Thank You,
Naik Durvesh.
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38766724
Thank you very much for your help.
regards
panos
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now