How to assign a string value to an index of php array? - php

I have the following error;
Note: Array to string conversion in [file_path] on line 919
which relates to this line of code where I'm trying to assign this string as a value in an array
$contents[11] = "$hours:$minutes:$seconds\n$ca_1[remaining_time]\n$h:$m:$s";
Why am I getting this error, and how do I resolve it?

It's a bad practice to interpolate string this way because it makes the code very difficult to read, so you should rather use "{$h}" instead of "$h".
As Terminus mentioned in comments, depending on the PHP version,
echo "$ca_1[remaining_time]"
Does not necessarily give a
PHP Notice: Use of undefined constant
Like echo $ca_1[remaining_time] would. But since that didn't work for you, you'd better quote that like ['remaining_time'].
You might also find some interesting things on this topic here.
Second, use curvy braces to explicitly tell what you want to insert:
$contents[11] = "$hours:$minutes:$seconds\n{$ca_1['remaining_time']}\n$h:$m:$s";
This really improves readability.

Try:
$contents[11] = $hours . ':' . $minutes . ':' . $seconds + "\n" . $ca_1['remaining_time'] . "\n " . $h . ':' . $m . ':' . $s";
If this still fails, check your variables. Maybe one of them is an array!?

Related

strpos() doesn't seem to be recognizing my the string I am using

I have the following string:
CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550
which in the code below corresponds to $track_matches[0][0].
The only constant-length field is the first (CAE33D8E804334D5B490EA273F36830A9849ACDF), which is 40 characters long. I am trying to get the values xx and yy which are an unknown length and value along with the rest of the column.
So I am trying something like this:
$seperator= '|';
$end_seed= strpos($track_matches[0][0], $seperator, 41 );
$seeders[$i] = substr($track_matches[0][0], 41, $end_seed - 41);
$end_leech= strpos($track_matches[0][0], $seperator, $end_seed +1 );
echo "end_seed" . $end_seed . " end_leach: " . $end_leech;
$leechers[$i] = substr($track_matches[0][0], $end_seed +1, $end_leech - $end_seed - 1);
The problem I am getting is the line $end_leech= doesn't seem to work properly (and doesn't recognize the $seperator) and retuns the entire line ($track_matches[0][0]) as it's value when echo'd while $end_seed returns the proper value. ... so what's going on why is this happening? howw do i fix it?
try:
$temp = explode("|", $track_matches[0][0]);
That will return an array and you can then reference the vars as $temp[1] (xx) and $temp[2] (yy)
try :
$myString="CAE33D8E804334D5B490EA273F36830A9849ACDF|xx|yy|46|13896|9550";
$splitString=explode('|',$myString);
$xx=$splitString[1];
$yy=$splitString[2];
of course you can replicate manually with strpos, substr etc but will take more effort

Trying To Clean Up My PHP Error Logs

I know that you're supposed to define PHP variables so that you don't clog up your error log with undefined variables, but my logs are still getting filled with unnecessary information.
For instance...
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined index: site in C:\Sites\FLCBranson.org\freedownloads.php on line 34
My PHP code has $site defined, but I do want to have option to override it...
// it's a good idea to define the variable first and then make changes as necessary (that way you don't fill up your logs with worthless errors)
$site = "flc";
// overrides the domain (useful for IP addresses)
if ($_GET["site"]) $site = $_GET["site"];
So, I have a lot of that type of issue. Then I have a bunch of these pesky errors...
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 3 in C:\Sites\FLCBranson.org\listseries.php on line 264
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 4 in C:\Sites\FLCBranson.org\listseries.php on line 265
[28-Jan-2013 16:45:42 UTC] PHP Notice: Undefined offset: 5 in C:\Sites\FLCBranson.org\listseries.php on line 266
I have an array that is populated with various bits of content. If something is in one of the slots, then I want to do something with it...
// explode() takes a string of text ($item->title in this case) and creates an array comprised of parts of the text separated by the separator (- in this case)
$title = explode(" - ", $sermontitle);
// sets the sermon title variable
$sermontitle = $title[0];
if ($title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if ($title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if ($title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if ($title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if ($title[5]) $sermontitle = $sermontitle . " - " . $title[5];
So, what am I doing incorrectly? I define my variable. Then I only make changes to the variable if certain conditions are met. I thought that was the appropriate way of doing it.
Edit...
I found another odd instance...
[28-Jan-2013 20:07:05 UTC] PHP Notice: Undefined variable: broadcast in C:\Sites\FLCBranson.org\flconlineservices.php on line 242
It seems that if (file_exists($golive) || ($broadcast == "live") isn't enough. Do I need to do if (file_exists($golive) || (isset($broadcast) && $broadcast == "live"))? That seems like a lot of code to perform a simple comparison.
Edit 2...
So, I'm starting to understand why isset() is required, but here's something that I don't get. I have some code pulling information from a database and I have if ($row["Sarasota"]) but the error log doesn't show a single thing for that. Why wouldn't isset() be required there if it is required for if ($title[5])? The only difference I can see is the quoted word "Sarasota" as opposed to the unquoted numeric 5.
Use isset to avoid these notice:
if (isset($title[1]) && $title[1]) $sermontitle = $sermontitle . " - " . $title[1];
if (isset($title[2]) && $title[2]) $sermontitle = $sermontitle . "<br>" . $title[2];
if (isset($title[3]) && $title[3]) $sermontitle = $sermontitle . " - " . $title[3];
if (isset($title[4]) && $title[4]) $sermontitle = $sermontitle . " - " . $title[4];
if (isset($title[5]) && $title[5]) $sermontitle = $sermontitle . " - " . $title[5];
A common way to write a default value is by using the ternary operator ? : like so:
$site = isset($_GET["site"]) ? $_GET["site"] : null; // null is the default value
For arrays I would recommend using the count function rather than isset. This reinforces the reader's understanding that you're dealing with an array.
isset is more comment used in associative arrays (like $_GET) so you might expect other keys that are not necessarily numeric.
if ($_GET["site"])
must be
if (isset($_GET["site"]))
And also for the $title things you should use isset:
if (isset($title[1])) [...]
You can also use a solution that encompasses isset's error handling, by using !empty
if(!empty($_GET['site']))

Dot operator in PHP

I thought I've known the String Operator . well enough until I was asked a question about it today. The question looks quite simple:
echo 100...100;
At the first glance I thought it would make a syntax error. But when I ran the code and saw the result I was totally confused. The result is
1000.1
So I wonder how could this happen?
Thanks.
Read it like this:
(100.) . (.100)
Thus it concats 100 and 0.1.
Assuming you meant
echo 100...100;
The reason for this is the beauty of PHP. :)
This statement is understood as
100. . .100
which is equivalent to
100.0 . 0.1
<=>
'100' . '0.1'
<=>
'1000.1'
You can read it as echo 100 . 0.1.
Actually, that only works without the quotes:
echo "100...100"; 100...100 << with quotes the . is just a char
echo 100 . 100; 100100 << two concatenated strings "100"
echo 100.100; 100.1 << 100.100 is just a number
echo 100...100; 1000.1 << what you asked
echo 100. . .100; 1000.1 << what PHP actually interprets

Obfuscating string values in PHP source code

I want to protect PHP source code at easy way.
here is a example.
$a = "\x46\122" . chr(578813952>>23) . "" . chr(0105) . "\x2d";
$b = "\x73" . chr(847249408>>23) . "" . chr(0162) . "\x69" . chr(0141) . "" . chr(905969664>>23) . "";
$c = "" . chr(0x53) . "" . chr(0105) . "\x52\x56" . chr(0105) . "\x52" . chr(796917760>>23) . "\x4e" . chr(545259520>>23) . "\x4d" . chr(0x45) . "";
it is.
$a="FREE-";
$b="serial";
$c="SERVER_NAME";
Please help me someone to convert this type of string ??
There is 3 type of encryption.
Type[1] : "\x46\122"
Type[2] : chr(0105)
Type[3] : chr(578813952>>23)
Please help me to create a convert function...from PHP string.
thank you !
------------------------ I update question-------------------
OK... I should change question..
I want to create a function.
function1.
$a = "FREE-";
echo function1($a);
---> output
"\x46\122" . chr(578813952>>23) . "" . chr(0105) . "\x2d";
in this function, Function use 3 type of logic at random.
here is 3 type.
Type[1] : "\x46\122"
Type[2] : chr(0105)
Type[3] : chr(578813952>>23)
Could you help me ....
This is a, frankly, stupid way of "protecting" your code. Hopefully you realize that once the code is delivered to the clients, they can simply undo all of this and extract the values themselves?
Use legal means to protect the code. "here's my code, you are not allowed to share it. If you do, I get $50 kazillion dollars and the Droit de Seigneur with your most beautiful daughter, or an extra 200 kazillion in lieue if they're all ugly".
An iron-clad licensing agreement will be far better protection than any cereal-box decoder-ring wet kleenex method you care to apply ever will be.
For further suggestions why this is a waste of your time:
Asked at 8:36, decoded at 8:44. Eight minutes of protection: https://stackoverflow.com/questions/5456462/what-does-this-php-code-do
Asked at 11:01, decoded at 11:17, and very-well analyzed at 11:47. Hacked, what does this piece of code do?
In the first case, I'm willing to bet the majority of the fastest poster's time was spent writing. So feel confident that however you try to obfuscate your code, it'll take only three or four minutes to undo whatever it is you've done.
How much time are you willing to put into obfuscating your code when it'll take someone only a few minutes to undo what you've done? Could that time have been better spent writing awesome features that your customers would love?
ord will get you a character's ASCII value, using that we can generate the 3 things you want.
Type 1: Use dechex to convert int to hex.
$chr = 's';
$hex = dechex(ord($chr)); // 73
Type 2: Use decoct to convert into to octal.
$chr = 'E';
$oct = decoct(ord($chr)); // 105
Type 3: Use the << (shift left) operator.
$chr = 'e';
$bin = ord($chr)<<23; // 847249408

PHP Advanced Currency Formatting

I was wondering if there is a simple method in PHP to format currency correctly for the following tasks:
If a value is: 4.37 then the output will be $4.37
If a value is: 4.00 then the output will be $4
If a value is: 4.3 or 4.30 then the output will be $4.30
If a value is 0.37 then the output will be 37¢
I'm sure this is quite complicated to do (I'm a beginner in PHP), but if anyone has any suggestions it would be greatly appreciated.
function format_currency($val) {
if ($val < 1) return intval(round($val * 100)) . '¢';
if (fmod($val, 1.0) == 0) return '$' . intval($val);
return '$' . intval($val) . '.' . intval(round((fmod($val,1))*100));
}
// Call it like this
$val = 1.2;
echo 'Your total: ' . format_currency($val);
Although this function will work, it's generally a bad idea to encode dollar amounts in a float.
I know this might be a bit of an overkill but take a look at Zend_Currency, it will take care of many different types of currency for this, it's also simple to use. Do note that you don't have to use the whole framework, just the currency class and the file it requires
http://framework.zend.com/manual/en/zend.currency.html

Categories