Solved

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

Posted on 2013-01-11
3
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

623 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