I am receiving the following Warning in my error log.
PHP Warning: Only the first byte will be assigned to the string offset in ....

The line it references is:
$item[$x]=substr($rowo->invid,1);

changed it to:
$item[$x]=substr($rowo->invid,1,NULL);

SAME result, the above warning.

looking in the php docuentation for substr it says the following:
If length is omitted or null, the substring starting from offset until the end of the string will be returned.
NOTE for v.8.0 -- length is nullable now. When length is explicitly set to null, the function returns a substring finishing at the end of the string, when it previously returned an empty string.

Seems to me my string should end up with position 1 to the end of the string. Why am I getting this warning??

    The warning is because $item is not an array. It is somehow defined as a string, where only one byte of it will be changed.

    pbismad

    Heh...got me curious, so...

    php > $foo = '';
    php > $foo[10] = substr('This is only a test', 1);
    php > var_export($foo);
    '          h'
    
      5 days later

      minifairy The $item was set as an array several lines up in the code

      Was anything done to $item between there and where the error is occurring now?

        You would need to post enough of the relevant code for someone to reproduce the problem.

        I was able to reproduce this error when $item was a string, using similar code to what NogDog posted, but it works without error and produces the expected result when $item is an array. You could use var_dump($item); immediately before the posted line code to see what $item is.

          Ok here is the code from the point where the $item is set as an array to the point where the value is inserted into the array in theory (where the warning is triggered.)

          if($pstat=='ac'){ #stat of 'ac' means its on account
          							#set up Payment options
          							#Accumulate Sale info into Arrays
          							$item=array();
          							$qty=array();
          							$option=array();
          							$optprice=array();
          							$desc=array();
          							$price=array();
          							$tprice=array();
          							$payopt='y';
          							$x='1';
          						}
          						echo "<h4 class=ct>Sale of: ".$saledate."<br>Payment Status: \n";
          						if($pstat=='ac'){
          							echo "<font color=red>On Account:</font>";
          						}
          						if($pstat=='pp')echo "PayPal";
          						if($pstat=='cc')echo "Credit Card";
          						if($pstat=='cs')echo "Cash";
          						if($pstat=='ck')echo "Check";
          						echo "<br>\n";
          						echo "Shipping Status: </b>";
          						if($sstat=='s')echo "Shipped";
          						if($sstat=='p')echo "Pending";
          						if($sstat=='h')echo "On Hold";
          						echo "</h4>\n";
          						if($_GET['pic']=='2')echo "<table class=cart>";#list page set up cart display
          					}#end set up new purchase
          					if($_GET['pic']=='1'){#Tumbnail page Detail
          						#array items for non paid invoices
          						if(substr($rowo->invid,1)=='BUCKS'){
          							$bucks=$rowo->tprice;
          						}else{
          							$item[$x]=substr($rowo->invid,1);
          							$qty[$x]=$rowo->qty;
          							$desc[$x]=cross_field('inventy','item','descrip','1',$rowo->invid);
          							$price[$x]=$rowo->price;
          							$tprice[$x]=$rowo->tprice;
          							if($rowo->opt!=''){
          								$opts=explode('|',$rowo->opt);
          								if($opts[0]=='-'){
          									$opt="-";
          								}else{
          									$opt=cross_field('options','id','title','1',$opts[0]);
          								}
          								if($opts[1]=='-'){
          									$opt.="-";
          								}else{
          									$opt.="<br>".cross_field('options','id','title','1',$opts[1]);
          								}
          							}
          							$option[$x]=$opt;
          						}
          

            The initialization is inside of a conditional statement, which was probably false, so the code was skipped over.

              Yeah, as pbismad points out, $item is only created if $pstat == "ac". So, if that condition is false, there's no $item to deal with and you'll end up with an error. I can't guarantee it's the error you're currently seeing because it's late and I'm tired, but I think it probably is.

              Well, I wrapped that code in this forum's [code]...[/code] tags to theoretically aid readability. If it really has that many levels of indenting, it might be time to refactor things. 😉

              Here's a reformatted version to fit within the page here:

              if ($something_to_balance_braces) {
                if ($pstat == 'ac') { #stat of 'ac' means its on account
                  #set up Payment options
                  #Accumulate Sale info into Arrays
                  $item = array();
                  $qty = array();
                  $option = array();
                  $optprice = array();
                  $desc = array();
                  $price = array();
                  $tprice = array();
                  $payopt = 'y';
                  $x = '1';
                }
                echo "<h4 class=ct>Sale of: " . $saledate . "<br>Payment Status: \n";
                if ($pstat == 'ac') {
                  echo "<font color=red>On Account:</font>";
                }
                if ($pstat == 'pp') echo "PayPal";
                if ($pstat == 'cc') echo "Credit Card";
                if ($pstat == 'cs') echo "Cash";
                if ($pstat == 'ck') echo "Check";
                echo "<br>\n";
                echo "Shipping Status: </b>";
                if ($sstat == 's') echo "Shipped";
                if ($sstat == 'p') echo "Pending";
                if ($sstat == 'h') echo "On Hold";
                echo "</h4>\n";
                if ($_GET['pic'] == '2') echo "<table class=cart>"; #list page set up cart display
              } #end set up new purchase
              if ($_GET['pic'] == '1') { #Tumbnail page Detail
                #array items for non paid invoices
                if (substr($rowo->invid, 1) == 'BUCKS') {
                  $bucks = $rowo->tprice;
                } else {
                  $item[$x] = substr($rowo->invid, 1);
                  $qty[$x] = $rowo->qty;
                  $desc[$x] = cross_field('inventy', 'item', 'descrip', '1', $rowo->invid);
                  $price[$x] = $rowo->price;
                  $tprice[$x] = $rowo->tprice;
                  if ($rowo->opt != '') {
                    $opts = explode('|', $rowo->opt);
                    if ($opts[0] == '-') {
                      $opt = "-";
                    } else {
                      $opt = cross_field('options', 'id', 'title', '1', $opts[0]);
                    }
                    if ($opts[1] == '-') {
                      $opt .= "-";
                    } else {
                      $opt .= "<br>" . cross_field('options', 'id', 'title', '1', $opts[1]);
                    }
                  }
                  $option[$x] = $opt;
                }
              } // final brace to balance things out
              

                All in all, it's definitely time to refactor. If nothing else, a switch statement is much easier to read than that many if statements...

                  Thanks everyone. I took the array assignments out of the conditional. hopefully that fixes it. I will look at this routine again and see if i can simplify it. I'll have to look for the info on switch, I'm not familiar with using it inside of a case. So you can nest them? switch is what is used to direct the flow of my entire program.

                  As for the code /code bit I thought that had been changed as in the menu it lists </> for insert code. sorry.

                    When all you are doing is mapping an input value to an output value, don't write out logic for each possible choice, which you have to find and edit any time you make a change to the mapping. Instead, define the choices in an array, with the index being the input value and the stored value being the output value. You can then simply check if an element matching the input value is set, get/echo the output value if it is, or get/echo a default value or an error message if it is not. You can then also use this defining array to dynamically produce the user interface where someone selects/edits the desired choice.

                      Write a Reply...