strlen() expects parameter 1 to be string, array given - php

I have an older site using phorm. When the form is submitted I get this error:
Warning: strlen() expects parameter 1 to be string, array given in /home/user/public_html/form/phorm.php on line 2015
I know this is because of an upgrade to php 5.3, is there an easy method to fix that line of code to make my forms work again?
if (strlen($PHORM_ALERTTO) && !strlen($PHORM_TO) && !$PHORM_INFONLY && !$ph_GotData) {
$PHORM_TO = $PHORM_ALERTTO; //THIS IS LINE 2015
Read the mail template file(s) and mail it (them) to the user */
$ph_section = "user template";
if (strlen($PHORM_ALERTTO) && !strlen($PHORM_TO) && !$PHORM_INFONLY && !$ph_GotData) {
$PHORM_TO = $PHORM_ALERTTO; //THIS IS LINE 2015
settype($PHORM_TO, "array");
}
if (isset($PHORM_TMPL) && isset($PHORM_TO) && !$ph_Abort) {
if ($ph_debug2) echo "<B>JS:</B> Mail Template(s)<BR>";
if (count($PHORM_TMPL) > $ph_MaxTMPL) $ph_Alerts['120'] = ph_Message("A120");
list(,$fPHORM_TO) = each($PHORM_TO);
list(,$fPHORM_SUBJECT) = each($PHORM_SUBJECT);
while ($ph_MaxTMPL-- && list($ph_key, $lPHORM_TMPL) = each($PHORM_TMPL)) {
if ($lPHORM_TMPL == ph_GENERIC) $lPHORM_TMPL = "$ph_root/files/generic.txt";
else $lPHORM_TMPL = "$ph_tpd/$lPHORM_TMPL";
$ph_Message = "";
$ph_Headers = "";
$ph_NonHeader = "";
$lPHORM_TO = "";
$lPHORM_FROM = "";
$lPHORM_SUBJECT = "";
$lPHORM_HEADERS = "";
$ph_TemplateHeaders = false;
if (ereg("(.+) +\+h$", $lPHORM_TMPL, $ph_regs)) {
$lPHORM_TMPL = trim($ph_regs[1]);
$ph_TemplateHeaders = true;
}
if ($ph_debug8) echo "Mail Template <B>$lPHORM_TMPL</B><BR>";
if (!$ph_Template = #implode("", file($lPHORM_TMPL))) {
$ph_Alerts['005'] = ph_Message("A005");
if ($php_errormsg) $ph_Alerts['005P'] = "%%%: $php_errormsg";
continue;
}

This is because your are using strlen() to count an array while it is supposed to use when you need to count strings. Your error is quite clear.
If you need to count an array or more just use count or sizeof functions.

Related

TypeError: DateTime::__construct() expects parameter 1 to be string, array given in DateTime->__construct() - Drupal 8

I am trying to save datecriteriamet date field to the backend and below is my function:
public function render(ResultRow $values) {
$id = $values->id;
$program = Program::load($id);
$progname = $program->getProgramName();
$lastreviewed = $program->getLastreviewed();
$criteriamet = $program->getcriteriamet();
$datecriteriamet = $program->getdatecriteriamet();
$path = Url::fromRoute('cca.show-program', ['cca_program' => $id])->toString();
if ($lastreviewed != '' && $datecriteriamet = '' && $criteriamet != 1) {
$date = date_create($lastreviewed);
$lastreviewed_date = $date->format('M Y');
return "<a href='${path}'></a>Last reviewed date: $lastreviewed_date";
} elseif ($datecriteriamet != '' && $criteriamet == 1) {
$criteriametdate = date_create($datecriteriamet);
$datecriteriamet_date = $criteriametdate->format('M Y');
return "<a href='${path}'></a>Date Criteria Met: $datecriteriamet_date
<br>
<br>
} else {
return "<a href='${path}'><button class='testclass2'>$progname</button></a>";
}
}
The function is returning data correctly and everything works fine when the date field is having a value. When datecriteriamet is NULL, I get to see:
TypeError: DateTime::__construct() expects parameter 1 to be string, array given in DateTime->__construct()
Not sure how to fix this. Can someone help?
Function date_create (an alias of DateTime::__construct()) requires that the first argument must be string. You need to make sure that function getdatecriteriamet() always returns a string.

PHP Pass variable from foreach to function

I'm trying to pass variables from a foreach function that fetch a mysql query to an array, to another function.
I declare the array before the foreach to make it global but no data are passed to the function.
here is my code. If you have any idea of what i'm doing, thanks by advance for your help.
Of course if I replace the called function by what's inside it, everything works like a charm.
but as I'll have to use it several times I would prefer to set my variables in a function.
function fillStuInfos() {
global $studFName, $studLName,c$dateStart, $dateEnd;
$studFName = $EventsGt['fName'];
$studLName = $EventsGt['lName'];
$dateStart = $EventsGt['startDate'];
$dateEnd = $EventsGt['endDate'];
}
$email = $_POST['email'];
$EventsGt = array();
$getEventsQry = 'SELECT * FROM Companies WHERE Email_Company = "'.$email.'" ORDER BY startDate';
foreach ($bddPDO->query($getEventsQry) as $EventsGt) {
$intCur = date_diff(date_create($today), date_create($EventsGt['endDate']));
$intFut = date_diff(date_create($today), date_create($EventsGt['startDate']));
echo intval($intCur->format('%r%a'));
if (intval($intCur->format('%r%a')) <= 4 && intval($intCur->format('%r%a')) > 0 ) {
fillStuInfos();
} else if ( intval($intFut->format('%r%a')) <= 4 && intval($intFut->format('%r%a')) > 0 ){
fillStuInfos();
} else {
echo 'No Datas!';
exit();
}
}
echo $studLName;
Forget that exists global and pass params to function.
function fillStuInfos($data) {
$studFName = $data['fName'];
$studLName = $data['lName'];
$dateStart = $data['startDate'];
$dateEnd = $data['endDate'];
}
...
foreach (...) {
if (intval($intCur->format('%r%a')) <= 4 && intval($intCur->format('%r%a')) > 0 ) {
fillStuInfos($EventsGt);
} else if ( intval($intFut->format('%r%a')) <= 4 && intval($intFut->format('%r%a')) > 0 ){
fillStuInfos($EventsGt);
} else {
echo 'No Datas!';
exit();
}
}
Your variable you want to be global is $EventsGt, not $studFName, $studLName, $dateStart, $dateEnd
With that said, I would recommend passing the event array as a parameter or passing the individual values as parameters instead.

PHP Fatal Error Cannot Use String Offset As Array

I keep getting a fatal error when a script is ran on my php server,
This web app was built ten years ago and I am currently clearing errors so we can start updating it. Current version PHP 5.2.17
Fatal error: Cannot use string offset as an array in /home/user/public_html/admin/script.php on line 1418
This is the line the error is on,
$name = $d["#"]["_Name"];
This is the full function,
if (isset($b["_BORROWER"]["EMPLOYER"])) {
if (!isset($b["_BORROWER"]["EMPLOYER"][0])) {
$temp = $b["_BORROWER"]["EMPLOYER"];
unset($b["_BORROWER"]["EMPLOYER"]);
$b["_BORROWER"]["EMPLOYER"][0] = $temp;
}
foreach($b["_BORROWER"]["EMPLOYER"] as $c => $d) {
$pid = '0';
// Finish up.
$item["type"] = "Personal";
$name = $d["#"]["_Name"];
//check for files in other bureaus
$query = doquery("SELECT name,id FROM <<myitems>> WHERE cid='".$client["id"]."' AND type='Personal'");
$results = dorow($query);
if($results){
if(isset($results['name'])){
$temp = $results;
unset($results);
$results[0] = array($temp);
}
foreach($results as $c){
if(isset($c['name'])){
if($address == decrypt_string($c['name'])) {
$pid = $c['id'];
break;
};
}
}
}
Does anyone understand what is triggering this error and how to fix it?
You can use isset() to check the array value exists or not like,
$name = isset($d["#"]["_Name"]) ? $d["#"]["_Name"] : "";

"Warning: in_array() expects at most 3 parameters, 5 given" when try upload file

I tried using some file input in a form for uploading several different file types and when clicking on the submit button then file multiple files are stored in a mysql table .
the problem is when clicking the sumbit it will display an error: "Warning : in_array ( ) expects at most 3 parameters , 5 given in....". when I use one input file , the file was successfully uploaded to the server folder .
and this is the code I used :
$dir = $name;
$target_dir = "my-file/$dir/";
if( is_dir($target_dir) === false )
{
mkdir($target_dir);
}
if(isset($_POST["submit"])) {
$formatfilea = array("pdf");
$formatfileb = array("pdf");
$formatfilec = array("pdf");
$formatfiled = array("jpg, jpeg");
$filea = $_FILES['filea']['name'];
$fileb = $_FILES['fileb']['name'];
$filec = $_FILES['filec']['name'];
$filed = $_FILES['filed']['name'];
$xa = explode('.', $filea);
$xb = explode('.', $fileb);
$xc = explode('.', $filec);
$xd = explode('.', $filed);
$existencea = strtolower(end($xa));
$existenceb = strtolower(end($xb));
$existencec = strtolower(end($xc));
$existenced = strtolower(end($xd));
$sizea = $_FILES['filea']['size'];
$sizeb = $_FILES['fileb']['size'];
$sizec = $_FILES['filec']['size'];
$sized = $_FILES['filed']['size'];
$file_tmp_a = $_FILES['filea']['tmp_name'];
$file_tmp_b = $_FILES['fileb']['tmp_name'];
$file_tmp_c = $_FILES['filec']['tmp_name'];
$file_tmp_d = $_FILES['filed']['tmp_name'];
if(in_array($existencea, $formatfilea && $existenceb, $formatfileb && $existencec, $formatfilec && $existenced, $formatfiled) === true){
if($sizea < 1044070 && $sizeb < 1044070 && $sizec < 1044070 && $sized < 1044070){
move_uploaded_file($file_tmp_a, $target_dir.$filea);
move_uploaded_file($file_tmp_b, $target_dir.$fileb);
move_uploaded_file($file_tmp_c, $target_dir.$filec);
move_uploaded_file($file_tmp_d, $target_dir.$filed);
} else {
}
}
}
// attempt insert query execution
$sql = "INSERT INTO test (filea, fileb, filec, filed) VALUES ('$filea', '$fileb', '$filec', '$filed')";
is there who can help me provide a solution? What causes this error? and why I uploaded file is not found on the file server?
You should refer to the syntax of in_array(). It accepts, at least two parameters.
in_array(param_1, param_2, param_3);
First one is the one you want to find.
Second one is the array.
Third parameter is optional. You can set it to true if you want to use strict checking so that it checks the type as well.
You just need to write your if statement this way:
if (in_array($existencea, $formatfilea, true) && in_array($existenceb, $formatfileb, true) && in_array($existencec, $formatfilec, true) && in_array($existenced, $formatfiled, true)) {
Note: Setting the third parameter to true will strengthen your code.
Refer to second example: in_array() strict checking
Replace
if(in_array($existencea, $formatfilea && $existenceb, $formatfileb && $existencec, $formatfilec && $existenced, $formatfiled) === true){
with
if (in_array($existencea, $formatfilea) && in_array($existenceb, $formatfileb) && in_array($existencec, $formatfilec) && in_array($existenced, $formatfiled)) {

debug_backtrace - long parameter

I have the following function:
function backtrace($Object=false)
{
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
But when I use it, I'm getting an error like below:
Warning: debug_backtrace() expects parameter 1 to be long, string given in /mypath/ on line 717 ---> foreach((array)debug_backtrace($Object) as $aVal)
What's causing the error? How can I fix it?
The first parameter of debug_backtrace() is a bitmask of options (i.e. a long). It is a simple boolean true/false in PHP versions prior to 5.3.6.
To fix it, either don't pass in the $Object variable you're currently passing in or update it to be any combination of the supported options that you want to be used.
Example:
$Object = DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT;
If you want to add a pre-condition to your current block of code that will set a default value if $Object is invalid, you could try something like:
function backtrace($Object = false) {
if (!is_long($Object) || (!($Object & DEBUG_BACKTRACE_PROVIDE_OBJECT) && !($Object & DEBUG_BACKTRACE_IGNORE_ARGS))) {
$Object = 0;
}
$x = 0;
foreach((array)debug_backtrace($Object) as $aVal) {
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}
for php >= 5.3.6, you should use bitmask options
function backtrace($Object=false) {
$x = 0;
foreach((array)debug_backtrace($Object ? DEBUG_BACKTRACE_PROVIDE_OBJECT : 0) as $aVal)
{
$row[$x]['file'] = $aVal['file'];
$row[$x]['line'] = $aVal['line'];
$row[$x]['function'] = $aVal['function'];
$row[$x]['class'] = $aVal['class'];
$row[$x]['args'] = $aVal['args'];
++$x;
}
return $row;
}

Categories