-2

i have the following code

@php

    $id = some integer
    echo  '<'.input type="hidden" class="id" name="id" value="$id". '>'
@endphp

how can i use this $id variable inside html element attribute

it produce the following error

Parse error: syntax error, unexpected '$id' (T_VARIABLE), expecting ',' or ';' (View: C:\xampp\htdocs\blog\resources\views\post\userposts.blade.php)

Sohel0415
  • 9,523
  • 21
  • 30
Hidayat ullah
  • 21
  • 2
  • 7
  • 1
    Your code is so full of parsing errors, that even when you fix that first one at least three others should occur one after another. This does not even look related to PHP code. Are you sure that this is not some template engine you are working with? – feeela Apr 12 '18 at 08:09

5 Answers5

4

You already are in a blade template, just use blade and its syntax:

@php
 $id = whatever you need
@endphp

<input type="hidden" class="id" name="id" value="{{ $id }}">

As you see you can use your variables with the special quotation {{ $var }}

javier_domenech
  • 5,995
  • 6
  • 37
  • 59
1

There are a lot of ways to do this. You just need to search it.

@php
   $id = some integer
@endphp

<input type="hidden" class="id" name="id" value="<?php echo $id; ?>">

or

@php

$id = some integer
echo  '<input type="hidden" class="id" name="id" value="'.$id.'">';

@endphp

or

@php
   $id = some integer
@endphp

<input type="hidden" class="id" name="id" value="{{ $id }}">

I suggest to call the $id from controller or the value of $id.

Onitech
  • 85
  • 1
  • 15
0

You forget to use semicolon at the end of the line:

$id = 111;// some integer
Pavel
  • 918
  • 7
  • 18
0

Your concatenation is wrong, you should pay attention to what you wrote, here is the good way of making it work:

$id = 5;
echo  '<input type="hidden" class="id" name="id" value="' . $id . '">';
Hammerbot
  • 15,696
  • 9
  • 61
  • 103
0

Either of these should work ..

<input type="hidden" class="id" name="id" value="<?=echo $id;?>"

OR

 <input type="hidden" class="id" name="id" value="<?php echo $id;?>"
Paddy Popeye
  • 1,634
  • 1
  • 16
  • 29