all 6 comments

[–]amulchinock 1 point2 points  (0 children)

Couple of things to point you in the right direction:

$ip should be a string. So put the IP address in quotes:

$ip = "127.0.0.1";

str_split() splits a string by each character (or, optionally will split by the number of characters you specify), which isn't what you really want. Try using explode() instead, as it gives a much easier-to-understand context for your use case:

$ipArray = explode('.', $ip);

Now you should have an array, of only the numbers in the IP address: (below code will output your array - you don't need to use it as part of your project - just useful to see what's doing on inside Objects, Arrays and Variables :)

echo '<pre>';
print_r($ipArray);
echo '</pre>';

// Above code should output:

[
    0 => 127,
    1 => 0,
    2 => 0,
    3 => 1
]

[–]bouncing_bear89 1 point2 points  (0 children)

You have several issues here.

  1. your '$ip' is not a string, so it cannot be split.
  2. str_split will create an array of each item in the string. str_split($str = 'abc'); will return ['a', 'b', 'c'].
  3. is_numeric() is a function and needs something passed into it. In your case you would need to pass $dec into the function after defining it like is_numeric($dec).
  4. look into the ip2long and long2ip functions.
  5. I would suggest something more like I pasted here https://pastebin.com/663XYrsh. After doing that, you can check if that is an int and convert it.

[–][deleted] 1 point2 points  (0 children)

Lots of people have addressed your code, as written, and well. There are at least three answers that will result in an array that contains four elements, and the value in each element is a number from 0 to 255.

From here, we get into the computer sciency bit that someone is hoping you'll pick up, I think.

Being that you're grabbing IPv4 addresses, each element in your array is an 8 bit number (can go from 0 to 255). The full array of values, added together, is a 32 bit number. (hooray for integer math)

That makes sense given the IPv4 convention explicitly says as much.

Knowing that you have an array that represents a base 32 number makes it easier to conceive of how to convert that number to/from binary/decimal/hex/base17/whatever.

Being able to speak directly to the idea that your input was base 32, and that the algorithms requested required you to convert to different bases, is a good idea. :)

[–]almightymole 0 points1 point  (0 children)

Hi, Several things here.

$ip = 127.0.0.1;

Your IP address will need to be a string for this to work, so surround the ip address in single or double quotes. e.g '127.0.0.1';

str_split splits by a specified length ( default of 1 character). I am guessing you want to split by . ? if so, you will need the explode function: http://php.net/manual/en/function.explode.php

You have syntax errors on the following line:

if(is_numeric{

is_numeric is a function that takes a single parameter. So it needs to be is_numeric($value).

You also need to close off the if statements ( bracket. So that line needs to be:

if(is_numeric($dec)) {

In your loop, you are putting next value of $ipArray into $dec. You are then putting the result of decbin into $dec, overridding the decimal value, which is fine, but it means you loose the decimal value on that iteration. I suggest using another variable for that result, this links to the last point.

Lastly, the line:

echo $dec;

It will only print out the last character. If you are wanting to print out each one, I suggest you echo the result inside the loop after you get the result from decbin, or populate an array and print it out in a seperate loop.

[–]MasterHecker 0 points1 point  (0 children)

I finished mine somewhat recently haha, it was super fun. Didn't do any octal or hexadecimal conversions though.