• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 519
  • Last Modified:

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

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
Panos
Asked:
Panos
2 Solutions
 
Brad HoweDevOps ManagerCommented:
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
 
d4durveshCommented:
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
 
PanosAuthor Commented:
Thank you very much for your help.
regards
panos
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now